'how to automatically open a website when launching the dash?

I am using plotly-dash with jupyter dash. I am wondering if I can automatically open a website if Jupyter dash is run and launch the dashboad after app.run_server(mode='external',debug=True,port=8050).

The reason is I have to log in a website to connect to the data for the dashboard.

Thanks



Solution 1:[1]

Dash runs on Flask in the background, so I found a similar question for Flask which can be adapted for dash similarly (credit to both responders to that question at the time of writing this answer).

Here is an example on how you can adapt it for Dash:

import os
from threading import Timer
import webbrowser

import dash
from dash import html
from dash import dcc

app = dash.Dash(__name__)

app.layout = html.Div(
    [
        dcc.DatePickerRange(id='date-range')
    ]
)

def open_browser():
    if not os.environ.get("WERKZEUG_RUN_MAIN"):
        webbrowser.open_new('http://127.0.0.1:1222/')

if __name__ == "__main__":
    Timer(1, open_browser).start()
    app.run_server(debug=True, port=1222)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Al Mouiee