Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
## [UNRELEASED]

### Added
- Add a Copy button to each error in the debug toolbar, which copies the error message and its traceback to the clipboard. Closes [#3624](https://github.com/plotly/dash/issues/3624).
- [#4203](https://github.com/plotly/dash/pull/4203) Add a Copy button to each error in the debug toolbar, which copies the error message and its traceback to the clipboard. Closes [#3624](https://github.com/plotly/dash/issues/3624).
- [#4204](https://github.com/plotly/dash/pull/4204) Add `DASH_HOST` and `DASH_PORT` environment variables to set the listen address for `Dash.run`, taking precedence over the generic `HOST` and `PORT` variables. Fixes [#3400](https://github.com/plotly/dash/issues/3400).

### Changed
- [#4118](https://github.com/plotly/dash/pull/4118) The JupyterLab extension now ships prebuilt, so JupyterLab 3 and 4 load it without asking for a rebuild. JupyterLab 2 is no longer supported. Fixes [#3828](https://github.com/plotly/dash/issues/3828).
Expand Down
12 changes: 7 additions & 5 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -2602,11 +2602,11 @@ def run(
too. Values provided here take precedence over environment variables.

:param host: Host IP used to serve the application, default to "127.0.0.1"
env: ``HOST``
env: ``DASH_HOST``, falling back to ``HOST``
:type host: string

:param port: Port used to serve the application, default to "8050"
env: ``PORT``
env: ``DASH_PORT``, falling back to ``PORT``
:type port: int

:param proxy: If this application will be served to a different URL
Expand Down Expand Up @@ -2734,11 +2734,13 @@ def run(
# Some conda systems has issue with setting the host environment
# to an invalid hostname.
# Related issue: https://github.com/plotly/dash/issues/3069
host = host or "127.0.0.1"
# DASH_HOST is still honored here: it is specific to Dash, so
# unlike the generic HOST it cannot come from another service.
host = host or os.getenv("DASH_HOST", "127.0.0.1")
else:
host = host or os.getenv("HOST", "127.0.0.1")
host = host or os.getenv("DASH_HOST", os.getenv("HOST", "127.0.0.1"))
assert host
port = port or os.getenv("PORT", "8050")
port = port or os.getenv("DASH_PORT", os.getenv("PORT", "8050"))
proxy = proxy or os.getenv("DASH_PROXY")

# Verify port value
Expand Down
66 changes: 66 additions & 0 deletions tests/unit/test_app_runners.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,72 @@ def test_process_server_smoke(dash_process_server):
os.chdir(cwd)


@pytest.mark.parametrize(
("env", "run_kwargs", "expected_host", "expected_port"),
[
# DASH_HOST/DASH_PORT win over the generic HOST/PORT.
(
{
"DASH_HOST": "0.0.0.0",
"DASH_PORT": "8060",
"HOST": "192.0.2.1",
"PORT": "8070",
},
{},
"0.0.0.0",
8060,
),
# Generic HOST/PORT still work when the DASH_ ones are absent.
({"HOST": "192.0.2.1", "PORT": "8070"}, {}, "192.0.2.1", 8070),
# Each DASH_ variable falls back independently.
({"DASH_HOST": "0.0.0.0", "PORT": "8070"}, {}, "0.0.0.0", 8070),
({"HOST": "192.0.2.1", "DASH_PORT": "8060"}, {}, "192.0.2.1", 8060),
# Defaults when no relevant variable is set.
({}, {}, "127.0.0.1", 8050),
# Explicit arguments win over environment variables.
(
{"DASH_HOST": "0.0.0.0", "DASH_PORT": "8060"},
{"host": "192.0.2.1", "port": 8070},
"192.0.2.1",
8070,
),
# DASH_HOST is honored even under conda, where the generic HOST
# is ignored because some conda systems set it to an invalid
# hostname (see #3069).
(
{"CONDA_PREFIX": "/opt/conda", "DASH_HOST": "0.0.0.0"},
{},
"0.0.0.0",
8050,
),
# ... and without DASH_HOST the conda workaround still applies.
(
{"CONDA_PREFIX": "/opt/conda", "HOST": "192.0.2.1"},
{},
"127.0.0.1",
8050,
),
],
)
def test_run_host_port_env_vars(
monkeypatch, env, run_kwargs, expected_host, expected_port
):
for var in ("DASH_HOST", "DASH_PORT", "HOST", "PORT", "CONDA_PREFIX"):
monkeypatch.delenv(var, raising=False)
for var, value in env.items():
monkeypatch.setenv(var, value)

app = dash.Dash(__name__)
run = Mock()
monkeypatch.setattr(app.backend, "run", run)

app.run(debug=False, **run_kwargs)

run.assert_called_once()
assert run.call_args.kwargs["host"] == expected_host
assert run.call_args.kwargs["port"] == expected_port


@pytest.mark.parametrize(
("server_type", "expected_options"),
[
Expand Down
Loading