Skip to content

Commit 6cd7f6f

Browse files
committed
Remove complicated autoreload_port offset logic
Now that we have random port numbers, just use those by default
1 parent 07c7643 commit 6cd7f6f

File tree

1 file changed

+12
-35
lines changed

1 file changed

+12
-35
lines changed

shiny/_main.py

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import importlib
33
import importlib.util
44
import os
5-
import re
65
import shutil
76
import sys
87
import types
@@ -59,9 +58,9 @@ def main() -> None:
5958
)
6059
@click.option(
6160
"--autoreload-port",
62-
type=str,
63-
default="+123",
64-
help="Bind autoreload socket to this port number. If the value begins with + or -, it will be added to the value of --port. Ignored if --reload is not used.",
61+
type=int,
62+
default=0,
63+
help="Bind autoreload socket to this port. If 0, a random port will be used. Ignored if --reload is not used.",
6564
show_default=True,
6665
)
6766
@click.option(
@@ -108,7 +107,7 @@ def run(
108107
app: Union[str, shiny.App],
109108
host: str,
110109
port: int,
111-
autoreload_port: str,
110+
autoreload_port: int,
112111
debug: bool,
113112
reload: bool,
114113
ws_max_size: int,
@@ -136,7 +135,7 @@ def run_app(
136135
app: Union[str, shiny.App] = "app:app",
137136
host: str = "127.0.0.1",
138137
port: int = 8000,
139-
autoreload_port: str = "",
138+
autoreload_port: int = 0,
140139
debug: bool = False,
141140
reload: bool = False,
142141
ws_max_size: int = 16777216,
@@ -165,11 +164,7 @@ def run_app(
165164
The port that the app should listen on. Set to 0 to use a random port.
166165
autoreload_port
167166
The port that should be used for an additional websocket that is used to support
168-
hot-reload; ignored if ``reload`` is False. If the value begins with ``"+"`` or
169-
``"-"``, it will be added to the value of ``port`` (unless ``port`` is 0, in
170-
which case, ``autoreload_port`` will also be chosen at random). If 0, a random
171-
port number will be used. If any other numeric value, that port number will be
172-
used.
167+
hot-reload. Set to 0 to use a random port.
173168
debug
174169
Enable debug mode.
175170
reload
@@ -213,11 +208,6 @@ def run_app(
213208
# If port is 0, randomize
214209
if port == 0:
215210
port = _utils.random_port(host=host)
216-
# If autoreload port is calculated relative to
217-
if reload and (
218-
autoreload_port.startswith("+") or autoreload_port.startswith("-")
219-
):
220-
autoreload_port = "0"
221211

222212
os.environ["SHINY_HOST"] = host
223213
os.environ["SHINY_PORT"] = str(port)
@@ -235,30 +225,17 @@ def run_app(
235225
else:
236226
reload_dirs = []
237227

238-
if reload and autoreload_port != "":
239-
m = re.search("^([+-]?)(\\d+)$", autoreload_port)
240-
if not m:
241-
sys.stderr.write(
242-
"Error: Couldn't understand the provided value for --autoreload-port\n"
243-
)
244-
exit(1)
245-
autoreload_port = str(_utils.random_port(host=host))
246-
autoreload_port_num = int(m.group(2))
247-
if m.group(1) == "+":
248-
autoreload_port_num += port
249-
elif m.group(1) == "-":
250-
autoreload_port_num = port - autoreload_port_num
251-
252-
if autoreload_port_num == 0:
253-
autoreload_port_num = _utils.random_port(host=host)
254-
255-
if autoreload_port_num == port:
228+
if reload:
229+
if autoreload_port == 0:
230+
autoreload_port = _utils.random_port(host=host)
231+
232+
if autoreload_port == port:
256233
sys.stderr.write(
257234
"Autoreload port is already being used by the app; disabling autoreload\n"
258235
)
259236
reload = False
260237
else:
261-
setup_hot_reload(log_config, autoreload_port_num, port, launch_browser)
238+
setup_hot_reload(log_config, autoreload_port, port, launch_browser)
262239

263240
if launch_browser and not reload:
264241
setup_launch_browser(log_config)

0 commit comments

Comments
 (0)