Skip to content

Commit

Permalink
Pick a random port number at startup (#59)
Browse files Browse the repository at this point in the history
* Pick a random port at startup
  • Loading branch information
bjornandre authored Sep 13, 2022
1 parent 8a482c1 commit dac8d31
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DataDoc.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
}
}
4 changes: 2 additions & 2 deletions datadoc/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
progress_bar,
)
from datadoc.frontend.components.VariablesTab import get_variables_tab
from datadoc.utils import running_in_notebook
from datadoc.utils import running_in_notebook, pick_random_port

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -90,7 +90,7 @@ def main(dataset_path: str = None):

JupyterDash.infer_jupyter_proxy_config()
app = build_app(JupyterDash)
app.run_server(mode="jupyterlab")
app.run_server(mode="jupyterlab", port=pick_random_port())
else:
# Assume running in server mode is better (largely for development purposes)
logger.debug("Starting in development mode")
Expand Down
9 changes: 9 additions & 0 deletions datadoc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ def get_display_values(
value = value.dict()[current_language.value]
return_dict[field_name] = value
return return_dict


def pick_random_port():
"""Pick a random free port number. The function will bind a socket to port 0, and
a random free port from 1024 to 65535 will be selected by the operating system."""
import socket
sock = socket.socket()
sock.bind(('', 0))
return sock.getsockname()[1]

0 comments on commit dac8d31

Please sign in to comment.