Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

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

Open
anastazie opened this issue May 29, 2023 · 1 comment

Comments

@anastazie
Copy link

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
Copy link

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)
`

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants