Handling POST request in multi-page app

I have seen examples of single-page apps that use @app.server.route('/post-endpoint', methods=['POST']) where app is something like app = dash.Dash(__name__), but in my case the app is multi-page and form submission is happening in one of the modules inside the pages folder, not in the “main” module, so that module does not have app = ..., it just has dash.register_page().

How do I “get” to the app’s server in that case?

Btw, the form is dynamic, so I can’t just write a callback with a list of State objects with properties like id and value known in advance; I am pretty sure that I need to use request.form to get the submitted data.

I wonder if

app = dash.get_app()

Might work for this?

1 Like

@jbeyer Yes, that worked.

Then it turned out that the company’s Dash wrapper I am using strips the server property. So I ended up not even creating a form component, but making sure the input/dropdown/etc controls have the same id pattern and using pattern-matching callbacks to get their state.

Pattern-Matching Callbacks | Dash for Python Documentation | Plotly uses it with callback Input objects, but it works for State objects as well.

Hello @maklai,

This is correct. If you need the POST in the future, you can always make the routes in the app.py after initialization, or, make a separate flask server and import that into the dash app, which can also be imported to all subsequent page files as well.

server.py

server = Flask(__name__)

app.py

from server import server


app = Dash(__name__, server=server)

pages…

from server import server

@server.route(path, methods)
1 Like

Thanks, @jinnyzor