Hi,
I am building a multi-page application with Dash. The structure of my application looks like this:
apps/app_1.py
apps/app_2.py
apps/app_3.py
index.py
app.py
I need to have an access to flask.request.headers at the moment when I am loading data in the given app (e.g app_1.py) and these request headers need to be passed from my router which is in index.py. Is it possible to achieve? If yes how to do that.
Here is my index.py file:
import flask
from dash.dependencies import Input, Output
from dash import html, dcc
from app import app, server
from apps import app_1, app_2, app_3
app.layout = html.Div(
[
dcc.Location(id="url", refresh=False),
html.Div(id="page-content", children=[]),
]
)
@app.callback(Output("page-content", "children"),
[Input("url", "pathname"),
Input("url",'href')
]
)
def display_page(pathname, href):
headers = flask.request.headers
if pathname == "/apps/app_1":
return app_1.layout
if pathname == "/apps/app_2":
return app_2.layout
if pathname == "/apps/app_3":
return app_3.layout
return ""
if __name__ == "__main__":
app.run_server(debug=True)
And here is example of app_1.py
import pandas as pd
data = pd.read_csv('file.csv')
# here I need to have acces
# data = data[data['auth']==values]
layout = [..]
@app.callback(
Output(component_id="bar-chart", component_property="figure"),
[Input(component_id="url", component_property='href')],
prevent_initial_call=False,
)
def update_graph(href):
params = parse_url_args(href)
if 'space' in params:
data = data[data['space'] == params['space']]
fig = px.bar(data, x="Name", y="Count")
return fig
So as you see I need to somehow access to headers variable which is set inside index.py in display_page() function, and this headers var needs to be accessible inside app_1.py at the stage after data = pd.read_csv(…)
For now my idea is to put everything inside app_1.py into function like create_app(headers)
and in display_page() pass headers into this function:
def display_page(pathname, href):
headers = flask.request.headers
if pathname == "/apps/app_1":
return create_app(headers)
if pathname == "/apps/app_2":
return app_2.layout
if pathname == "/apps/app_3":
return app_3.layout
return ""
Do you know any other way how to achieve that? Maybe I can have access to request headers from the app.server instance of dash ?