Skip to content

Commit

Permalink
add port argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Oddant1 committed Dec 31, 2024
1 parent 5521b1b commit 6b6cba5
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions q2cli/builtin/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,16 @@ def _merge_metadata(paths):
return metadata


def get_free_port():
"""Finds an available port on the system.
"""
import socket

with socket.socket() as _socket:
_socket.bind(('localhost', 0))
return _socket.getsockname()[1]


@tools.command(short_help='View a QIIME 2 Visualization.',
help="Displays a QIIME 2 Visualization until the command "
"exits. To open a QIIME 2 Visualization so it can be "
Expand All @@ -521,7 +531,10 @@ def _merge_metadata(paths):
@click.option('--index-extension', required=False, default='html',
help='The extension of the index file that should be opened. '
'[default: html]')
def view(visualization_path, index_extension):
@click.option('--port', required=False, type=click.IntRange(1024, 49151),
default=get_free_port(),
help='The port the to serve the webapp on.')
def view(visualization_path, index_extension, port):
# Guard headless envs from having to import anything large
import sys

Expand Down Expand Up @@ -575,19 +588,9 @@ def do_GET(self):
else:
super().do_GET()

def get_free_port():
"""Finds an available port on the system.
"""
import socket

with socket.socket() as _socket:
_socket.bind(('localhost', 0))
return _socket.getsockname()[1]

VENDOR_PATH = '/home/anthony/src/qiime2/q2view/vendored/'

# Start server
port = get_free_port()
server = HTTPServer(('', port),
lambda *_: Handler(*_, directory=VENDOR_PATH))
click.echo(f'Agent started on port: {port}')
Expand Down

0 comments on commit 6b6cba5

Please sign in to comment.