How can I dynamically instantiate multiple apps through django plotly dash?

I am using https://django-plotly-dash.readthedocs.io/en/latest/index.html to build a dash app.

I want to dynamically create a new app when the symbol is different. I think this will create a new entry in django_plotly_dash_statelessapp table. How can I achieve this?

Example:

@app.callback(
    Output("example-graph", "figure"),
    [Input("input-element-id", "value")]
)
def update_figure(input_value, session_state=None, **kwargs):
    symbol = session_state["symbol"]  # Each unique symbol creates a different app instance
    new_figure = {
        "data": [
            {"x": [1, 2, 3], "y": [input_value, input_value + 1, input_value + 2], "type": "bar"}
        ],
        "layout": {"title": f"Updated Chart for {symbol}"}
    }
    return new_figure

Call in my view:

class SomeDetailView(DetailView):
    model = SomeModel 
    context_object_name = "detail"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        
        # Access the current session and add `symbol` to it
        session = self.request.session
        django_plotly_dash_objs = session.get("django_plotly_dash", {})
        
        django_plotly_dash_objs["symbol"] = context["detail"].symbol
        
        session["django_plotly_dash"] = django_plotly_dash_objs
        
        return context
Back to Top