plotly / jupyter-dash

OBSOLETE - Dash v2.11+ has Jupyter support built in!
https://dash.plotly.com/dash-in-jupyter
MIT License
955 stars 234 forks source link

OSError: Address 'http://127.0.0.1:8050' already in use. when using in Jupyter notebook #119

Open anastazie opened 1 year ago

anastazie commented 1 year ago

Hello, When I run code cell second time in the notebook, I am getting OSError: Address 'http://127.0.0.1:8050' already in use. The only thing that helps is restarting kernel. Is this an expected behaviour? If so, it makes it hard to use jupyter-dash to develop app.

jupyter-dash 0.4.2 Ubuntu 22.04

Example script

from dash import dcc, html
from jupyter_dash import JupyterDash as Dash
app = Dash(__name__)
app.layout = html.Div([
    html.Div(children='Hello World')
])
app.run_server(debug=True)
CallumLevon commented 1 year ago

Hey, I also had this issue, chatGPT gave me the below which works for me.

`import socket from contextlib import closing

def find_free_port(): with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: s.bind(('',0)) # '' means localhost and 0 means to find a free port s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) return s.getsockname()[1]

Then you can use this function when you run your Dash app

if name == 'main': port = find_free_port() app.run_server(debug=True, port=port) `