Python Panel 仪表板 – 多页面应用程序未按预期更新。

huangapple go评论99阅读模式
英文:

Python Panel Dashboard - Multi-Page app not updating as expected

问题

I want a Python Panel app which shows different page content based on a radio button selection in the sidebar.

There's some example code for doing exactly this in the way I want, shown as "Option 3" here:
https://discourse.holoviz.org/t/multi-page-app-documentation/3108/3?u=maxpower

以下是代码部分:

  1. import panel as pn
  2. pn.extension(sizing_mode="stretch_width")
  3. pages = {
  4. "Page 1": pn.Column("# Page 1", "...bla bla bla"),
  5. "Page 2": pn.Column("# Page 2", "...more bla"),
  6. }
  7. def show(page):
  8. return pages[page]
  9. starting_page = pn.state.session_args.get("page", [b"Page 1"])[0].decode()
  10. page = pn.widgets.RadioButtonGroup(
  11. value=starting_page,
  12. options=list(pages.keys()),
  13. name="Page",
  14. sizing_mode="fixed",
  15. button_type="success",
  16. )
  17. ishow = pn.bind(show, page=page)
  18. pn.state.location.sync(page, {"value": "page"})
  19. ACCENT_COLOR = "#0072B5"
  20. DEFAULT_PARAMS = {
  21. "site": "Panel Multi Page App",
  22. "accent_base_color": ACCENT_COLOR,
  23. "header_background": ACCENT_COLOR,
  24. }
  25. pn.template.FastListTemplate(
  26. title="As Single Page App",
  27. sidebar=[page],
  28. main=[ishow],
  29. **DEFAULT_PARAMS,
  30. ).servable()

然而,它似乎并没有按预期工作,至少在 panel v1.0.0rc6 上不是这样。当我点击 "Page 2",然后点击返回到 "Page 1" 时,主内容并没有如预期地切换回来。奇怪的是,URL 在 "Page+1" 和 "Page+2" 之间按预期切换,但主内容本身没有。

我一直在尝试使用 panel serve app.py --autoreload 进行调试,这是一个不错的工作流程,但我仍然没有能够弄清楚为什么它不起作用,或者如何找到一个使其工作的好解决方案。

英文:

I want a Python Panel app which shows different page content based on a radio button selection in the sidebar.

There's some example code for doing exactly this in the way I want, shown as "Option 3" here:
https://discourse.holoviz.org/t/multi-page-app-documentation/3108/3?u=maxpower

Code below:

  1. import panel as pn
  2. pn.extension(sizing_mode="stretch_width")
  3. pages = {
  4. "Page 1": pn.Column("# Page 1", "...bla bla bla"),
  5. "Page 2": pn.Column("# Page 2", "...more bla"),
  6. }
  7. def show(page):
  8. return pages[page]
  9. starting_page = pn.state.session_args.get("page", [b"Page 1"])[0].decode()
  10. page = pn.widgets.RadioButtonGroup(
  11. value=starting_page,
  12. options=list(pages.keys()),
  13. name="Page",
  14. sizing_mode="fixed",
  15. button_type="success",
  16. )
  17. ishow = pn.bind(show, page=page)
  18. pn.state.location.sync(page, {"value": "page"})
  19. ACCENT_COLOR = "#0072B5"
  20. DEFAULT_PARAMS = {
  21. "site": "Panel Multi Page App",
  22. "accent_base_color": ACCENT_COLOR,
  23. "header_background": ACCENT_COLOR,
  24. }
  25. pn.template.FastListTemplate(
  26. title="As Single Page App",
  27. sidebar=[page],
  28. main=[ishow],
  29. **DEFAULT_PARAMS,
  30. ).servable()
  31. ```import panel as pn
  32. pn.extension(sizing_mode="stretch_width")
  33. pages = {
  34. "Page 1": pn.Column("# Page 1", "...bla bla bla"),
  35. "Page 2": pn.Column("# Page 2", "...more bla"),
  36. }
  37. def show(page):
  38. return pages[page]
  39. starting_page = pn.state.session_args.get("page", [b"Page 1"])[0].decode()
  40. page = pn.widgets.RadioButtonGroup(
  41. value=starting_page,
  42. options=list(pages.keys()),
  43. name="Page",
  44. sizing_mode="fixed",
  45. button_type="success",
  46. )
  47. ishow = pn.bind(show, page=page)
  48. pn.state.location.sync(page, {"value": "page"})
  49. ACCENT_COLOR = "#0072B5"
  50. DEFAULT_PARAMS = {
  51. "site": "Panel Multi Page App",
  52. "accent_base_color": ACCENT_COLOR,
  53. "header_background": ACCENT_COLOR,
  54. }
  55. pn.template.FastListTemplate(
  56. title="As Single Page App",
  57. sidebar=[page],
  58. main=[ishow],
  59. **DEFAULT_PARAMS,
  60. ).servable()

However, it does not quite work as expected, at least not on panel v1.0.0rc6. When I click "Page 2" and then click back to "Page 1", the main content does not switch back as expected. Weirdly, the URL does switch between ...=Page+1 and ...=Page+2 as expected, but not the main content itself.

I've been trying to debug this using panel serve app.py --autoreload, which is a nice workflow, but I still haven't been able to figure out why this isn't working, or a good solution to get it working.

答案1

得分: 1

以下是您提供的内容的中文翻译:

答案位于此处,正是我所寻找的,并且我已经确认在 Panel 的 v1.0.0rc6 版本中正常工作:

  1. import panel as pn
  2. from panel.template import FastListTemplate
  3. pn.extension()
  4. # 将页面定义为类
  5. class Page1:
  6. def __init__(self):
  7. self.content = pn.Column("# 页面 1", "这是页面 1 的内容。")
  8. def view(self):
  9. return self.content
  10. class Page2:
  11. def __init__(self):
  12. self.content = pn.Column("# 页面 2", "这是页面 2 的内容。")
  13. def view(self):
  14. return self.content
  15. # 实例化页面并将它们添加到页面字典中
  16. pages = {
  17. "页面 1": Page1(),
  18. "页面 2": Page2(),
  19. }
  20. # 用于显示所选页面的函数
  21. def show_page(page_instance):
  22. main_area.clear()
  23. main_area.append(page_instance.view())
  24. # 定义用于在页面之间导航的按钮
  25. page1_button = pn.widgets.Button(name="页面 1", button_type="primary")
  26. page2_button = pn.widgets.Button(name="页面 2", button_type="primary")
  27. # 设置按钮单击回调
  28. page1_button.on_click(lambda event: show_page(pages["页面 1"]))
  29. page2_button.on_click(lambda event: show_page(pages["页面 2"]))
  30. # 创建侧边栏
  31. sidebar = pn.Column(page1_button, page2_button)
  32. # 创建主区域并显示第一个页面
  33. main_area = pn.Column(pages["页面 1"].view())
  34. # 创建 Material 模板并设置侧边栏和主区域
  35. template = FastListTemplate(
  36. title="多页面应用",
  37. sidebar=[sidebar],
  38. main=[main_area],
  39. )
  40. # 提供 Panel 应用程序
  41. template.servable()

请注意,我已经将代码中的 HTML 标记翻译为适当的中文。

英文:

The answer given here is exactly what I was looking for, and I've confirmed works as expected in Panel's v1.0.0rc6:

  1. import panel as pn
  2. from panel.template import FastListTemplate
  3. pn.extension()
  4. # Define pages as classes
  5. class Page1:
  6. def __init__(self):
  7. self.content = pn.Column("# Page 1", "This is the content of page 1.")
  8. def view(self):
  9. return self.content
  10. class Page2:
  11. def __init__(self):
  12. self.content = pn.Column("# Page 2", "This is the content of page 2.")
  13. def view(self):
  14. return self.content
  15. # Instantiate pages and add them to the pages dictionary
  16. pages = {
  17. "Page 1": Page1(),
  18. "Page 2": Page2(),
  19. }
  20. # Function to show the selected page
  21. def show_page(page_instance):
  22. main_area.clear()
  23. main_area.append(page_instance.view())
  24. # Define buttons to navigate between pages
  25. page1_button = pn.widgets.Button(name="Page 1", button_type="primary")
  26. page2_button = pn.widgets.Button(name="Page 2", button_type="primary")
  27. # Set up button click callbacks
  28. page1_button.on_click(lambda event: show_page(pages["Page 1"]))
  29. page2_button.on_click(lambda event: show_page(pages["Page 2"]))
  30. # Create the sidebar
  31. sidebar = pn.Column(page1_button, page2_button)
  32. # Create the main area and display the first page
  33. main_area = pn.Column(pages["Page 1"].view())
  34. # Create the Material Template and set the sidebar and main area
  35. template = FastListTemplate(
  36. title="Multi-Page App",
  37. sidebar=[sidebar],
  38. main=[main_area],
  39. )
  40. # Serve the Panel app
  41. template.servable()

huangapple
  • 本文由 发表于 2023年5月11日 04:37:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76222383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定