What is the proper way to handle user-defined app configuration options from a file in Plotly Dash? Specifically, I would like for these options to be loaded/accessible when the Dash
object is being instantiated so that I can pass arguments to its constructor (e.g. url_base_pathname
when instantiating Dash
app object), as well as be accessible inside of callbacks.
In Flask, according to its documentation, we have this method:
Configuration Handling — Flask Documentation (2.3.x) (palletsprojects.com)
I tried to check the Dash
object if it has Flask’s from_file
method and found that it doesn’t have it. (So I can’t follow what’s outlined in the Flask docs)
If I was in a hurry and only cared about the short-term, I would just create a global variable at the top of my app.py
file that loads in the data from the json
file and call it a day. However, we have this in Plotly Documentation: Why Global Variables will break your app
So I want to confirm with the community/subject-matter-experts what indeed is the proper approach.
Minimum Reproducible Example of what I’m talking about:
import json
from dash import Dash, html, callback, Input, Output
# with open("config.json", "r") as file:
# config = json.load(file)
# Actually loaded from the file using the above commented code
config = {
"option_1": "something1",
"url_base_pathname": "/somebasepath/"
}
app = Dash(
__name__,
url_base_pathname=config['url_base_pathname']
)
app.layout = html.Div(
children=[
html.P(
id='paragraph-1',
children=f"Hello World"
),
html.P(
id='paragraph-2',
children=""
)
]
)
@callback(
Output('paragraph-2','children'),
Input('paragraph-1', 'children')
)
def update_paragraph_2(p_1_child):
return config['option_1']
if __name__ == "__main__":
app.run()