My question is based on the solution posted about multipage design with panel
.
I would like to make the code a bit easier to maintain with pages being added. Therefore I used a dictionary. Something with the lambda
function seems to be wrong because any link points to page 2. Can anybody explain to me what I am doing wrong?
Here is the code
# app.py
import panel as pn
from panel.template import FastListTemplate
from pages.page_1 import Page1
from pages.page_2 import Page2
pn.extension()
def show_page(page_instance):
main_area.clear()
main_area.append(page_instance.view())
pages = {
"Page 1": Page1(),
"Page 2": Page2()
}
page_buttons = {}
for page in pages:
page_buttons[page] = pn.widgets.Button(name=page, button_type="primary")
page_buttons[page].on_click(lambda event: show_page(pages[page]))
# page1_button, page2_button = page_buttons.values()
# # Set up button click callbacks
# page1_button.on_click(lambda event: show_page(pages["Page 1"]))
# page2_button.on_click(lambda event: show_page(pages["Page 2"]))
sidebar = pn.Column(*page_buttons.values())
main_area = pn.Column(pages["Page 1"].view())
template = FastListTemplate(
title="Multi-Page App",
sidebar=[sidebar],
main=[main_area],
)
template.servable()
# pages/page_1.py
import panel as pn
class Page1:
def __init__(self):
self.content = pn.Column("# Page 1", "This is the content of page 1.")
def view(self):
return self.content
# pages/page_2.py
import panel as pn
class Page2:
def __init__(self):
self.content = pn.Column("# Page 2", "This is the content of page 2.")
def view(self):
return self.content
Thank you very much for your help.