0

I have done flask integration with Dash (credit to source) with file structure as below...

/<main_app>
     |
     |
     - <app>
     |  |-----__init__.py
     |
     - <dashboard>
     |  |--------Dash_App1.py
     |  |--------<dash_app_code1>
     |  |--------<dash_app_code2>
     |
     - <configs>
     |
     ----- run.py

run.py has following code...

from app import create_app
app = create_app(config_mode)
if __name__ == "__main__":
    app.run(debug = True)

"init.py" of the directory "app" has following code...

def create_app(config, selenium=False):
    app = Flask(__name__, static_folder='base/static')
    app.config.from_object(config)
    register_extensions(app)
    register_blueprints(app)
    app = Dash_App1.Add_Dash(app)
    return app

So I instantiate my Dash app in "Dash_App1.py" in "dashboard" directory like this.

def Add_Dash(server):
    d_app = Dash(server=server, url_base_pathname=url_base)
    apply_layout_with_auth(d_app, layout)

    @d_app.callback(Output('tabs-content', 'children'), Input('tabs', 'value'))
    def render_content(tab):
    if tab == 'tab-1':
        output = general_layout()
        return output

    @d_app.callback(Output('date-text', 'children'), Input('btn-cboe-data', 'n_clicks'))
    def readDaily(cobe_btn):
       if cobe_btn:
           do_something()

    return d_app.server

Here is my problem....Seems this design is forcing me to have all the callbacks in Add_Dash function in Dash_App1.py file. I want to organize my callbacks based on functionality in directories <dash_app_code1> and <dash_app_code2> in different files. But I don't know how to do this. I tried using "from flask import current_app" and tried to add callbacks there but it doesn't work. I tried assigning dash instance (d_app) to the global variable "g" and tried to import (and use) that in <dash_app_code1> but it throws out of context error. How can I add more dash related callbacks in files in different directories (<dash_app_code1> and <dash_app_code2> and avoid having all in Add_Dash function ?

LuckyStarr
  • 1,465
  • 2
  • 12
  • 14
  • 1
    This post might help [How to define callbacks in separate files?](https://stackoverflow.com/questions/62102453/how-to-define-callbacks-in-separate-files-plotly-dash) – EricLavault Aug 31 '23 at 13:29

1 Answers1

0

EricLavault's comment above has my answer. I used @dash.callback. This way, I didn't need app context.

LuckyStarr
  • 1,465
  • 2
  • 12
  • 14