How to send some values through url from a flask app to dash app ? flask and dash app are running independently
Asked Answered
P

2

6

I have a flask app running at one end and a dash app both are running separately but I have a link in the flask app home on clicking which redirects to dash app but I want to pass some values such as current user_id to dash app when while redirecting to dash app, then I want to read that value from URL and then I can display it dash home page.

I request if some can help me, please help me out.

Parisparish answered 29/6, 2021 at 17:33 Comment(3)
If it's a link, then just add parameters like you'd get in a GET request: <a link="https://www.example.com/dash/otherapi?userid=17116">.Hallucinosis
but how can i read those argument values in dash appParisparish
I assumed your "dash" app was listening as a web server too. Dash is very flexible; there are lots of ways to get data in. How do you get data into it now?Hallucinosis
H
4

The search parameter from location will return the URL params as string, pathname did not work for me as stated in @xhlulu answer:

@app.callback(Output('page-content', 'children'),
              Input('url', 'search'))  # <--- this has changed
def display_page(params):
    # e.g. params = '?firstname=John&lastname=Smith&birthyear=1990'
    parsed = urllib.parse.urlparse(params)
    parsed_dict = urllib.parse.parse_qs(parsed.query)

    print(parsed_dict)
    # e.g. {'firstname': ['John'], 'lastname': ['Smith'], 'birthyear': ['1990']}

    # use parsed_dict below
    # ...

   return page_content
Hygro answered 5/1, 2022 at 23:54 Comment(1)
'pathname' will ignore any search criteria. 'search' will ignore the path. This input criteria will get both. @app.callback(Output('url-content', 'children'), [Input('url', 'pathname'), Input('url', 'searchdata')]) def display_page(pathname,searchdata): ...Wildfowl
L
1

This Dash tutorial page explains how to handle URLs using dcc.Location. You can obtain the pathname as a callback input, and use a library like urllib to parse it.

This snippet is adapted from one the examples and this StackOverflow thread:

import urllib.parse

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

app = dash.Dash(__name__)

app.layout = html.Div([
    dcc.Location(id='url', refresh=False),
    html.Div(id='page-content')
])


@app.callback(Output('page-content', 'children'),
              Input('url', 'pathname'))
def display_page(pathname):
    if pathname.startswith("my-dash-app"):
        # e.g. pathname = '/my-dash-app?firstname=John&lastname=Smith&birthyear=1990'

        parsed = urllib.parse.urlparse(pathname)
        parsed_dict = urllib.parse.parse_qs(parsed.query)
        
        print(parsed_dict)
        # e.g. {'firstname': ['John'], 'lastname': ['Smith'], 'birthyear': ['1990']}

        # use parsed_dict below
        # ...

        return page_content

if __name__ == '__main__':
    app.run_server(debug=True)
Lemur answered 30/6, 2021 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.