I’m using Multi-Page Apps with query strings. It works well for the loaded sub-layouts, but I was wondering if there is any way to get the query string values into the main (shared) part of the layout too?
E.g. here code in app.py:
import dash
from dash import html
dash.register_page(__name__, name="department", path="/department")
def layout(department_id=None, **query_parameters):
return html.Div(
children=[
html.Div(children=f'''
This is department: {department_id}.
'''),
])
When opening the webpage with link like: http://127.0.0.1:1032/department?number=111&department_id=1
“department_id” is correctly displayed in the layout in the department.py, but I would also like to get the “number” to be displayed in the main layout part in app.py. I know I could achieve this using pattern-matched callbacks, but I hope there is a cleaner and more straightforward way.
You can use a dcc.Store to accomplish this. Pass the query string values to the dcc.Store and then upon update to the store, you change the elements in the app’s layout.
This would still require pattern matched callbacks though? Considering I want to have multiple pages - there is still the limitation of only one callback in all layouts being allowed to write to component (would be great if there would be a way around this!)
There is the multiplexer option, by dash-extensions for multiple callbacks to the same output.
Or, you could just have 1 callback on the app.layout which listens to the url and search as well. The location used for dash pages is, _pages_location.
This is a great question! I’ll add a minimal example to the dash-multi-page-app-demos github, but in the meantime, you can do this by keeping the fixed content in app.py file (ie the content that shows on every page like the header and footer), then put the rest of the content for the home page in a file in the pages folder. For example, you could call it home.py and register that page with path=“/”
dash.register_page(__name__, path="/")
This is how it’s done in the Dash Example Index. See this link: Dash
@AnnMarieW
I checked the example, and I understand how it loads query string values into the main page, but what I’m trying to achieve is to load query string values into both the main page (shared components, e.g. dropdowns) as well as a module sub-page. A structure like:
where both home.py and module1.py (assume many modules) would be able to get the data they need from the query string
@jinnyzor
I was just implementing your suggestion about having separate callback listening for query string inputs in the main page and it works great, thank you!
here new app.py for anyone looking for this solution: