How to get the url name on which the application is running?

Viewed 44

I have a Plolty Dash application which is running on localhost. I will be making it live and give a specific domain name. What I want to achieve is to get the url name on which the application is running. For instance, if it is running on localhost:8050, I want a Python script to grab that specific url and store it in a variable inside the application. Is there any way I can achieve this? I have searched the internet but what I get is Webscraping tutorials. I want the script to automatically detect the url on which it is running. Thank you for any help.

1 Answers

You need to add a Location() element from Dash Core Components to your layout. You then create a callback which takes that elements as an input and the URL information will be passed into the callback.

Here is a minimal example:

import dash_core_components as dcc
from dash import Dash, html, Input, Output

app = Dash()
app.layout = html.Div(
        children=[
            dcc.Location(id='uri', refresh=False),
            html.P(id='url-display')
        ]
    )

@app.callback(
    Output('url-display', 'children'),
    Input('uri', 'href')
)
def process_location(uri):
    return uri

if __name__ == "__main__":
    app.run_server(debug=False)
Related