Skip to content

Commit 6b6cba5

Browse files
committed
add port argument
1 parent 5521b1b commit 6b6cba5

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

q2cli/builtin/tools.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,16 @@ def _merge_metadata(paths):
511511
return metadata
512512

513513

514+
def get_free_port():
515+
"""Finds an available port on the system.
516+
"""
517+
import socket
518+
519+
with socket.socket() as _socket:
520+
_socket.bind(('localhost', 0))
521+
return _socket.getsockname()[1]
522+
523+
514524
@tools.command(short_help='View a QIIME 2 Visualization.',
515525
help="Displays a QIIME 2 Visualization until the command "
516526
"exits. To open a QIIME 2 Visualization so it can be "
@@ -521,7 +531,10 @@ def _merge_metadata(paths):
521531
@click.option('--index-extension', required=False, default='html',
522532
help='The extension of the index file that should be opened. '
523533
'[default: html]')
524-
def view(visualization_path, index_extension):
534+
@click.option('--port', required=False, type=click.IntRange(1024, 49151),
535+
default=get_free_port(),
536+
help='The port the to serve the webapp on.')
537+
def view(visualization_path, index_extension, port):
525538
# Guard headless envs from having to import anything large
526539
import sys
527540

@@ -575,19 +588,9 @@ def do_GET(self):
575588
else:
576589
super().do_GET()
577590

578-
def get_free_port():
579-
"""Finds an available port on the system.
580-
"""
581-
import socket
582-
583-
with socket.socket() as _socket:
584-
_socket.bind(('localhost', 0))
585-
return _socket.getsockname()[1]
586-
587591
VENDOR_PATH = '/home/anthony/src/qiime2/q2view/vendored/'
588592

589593
# Start server
590-
port = get_free_port()
591594
server = HTTPServer(('', port),
592595
lambda *_: Handler(*_, directory=VENDOR_PATH))
593596
click.echo(f'Agent started on port: {port}')

0 commit comments

Comments
 (0)