Skip to content

Commit

Permalink
feat: Allow automatic opening in browser.
Browse files Browse the repository at this point in the history
  • Loading branch information
mturoci committed Sep 4, 2023
1 parent 4013e2b commit 08970d8
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions py/h2o_wave/h2o_wave/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from pathlib import Path
from urllib import request
from urllib.parse import urlparse
import webbrowser

import click
import httpx
Expand Down Expand Up @@ -314,7 +315,8 @@ def learn():
@click.option('--subdomain', default='my-app', help='Subdomain to use. If not available, a random one is generated.')
@click.option('--remote-host', default='h2oai.app', help='Remote host to use (defaults to h2oai.app).')
@click.option('--remote-port', default=443, help='Remote port to use (defaults to 443).')
def share(port: int, subdomain: str, remote_host: str, remote_port: int):
@click.option('--open', is_flag=True, default=False, help='Open the shared app in your browser automatically.')
def share(port: int, subdomain: str, remote_host: str, remote_port: int, open: bool):
"""Share your locally running app with the world.
\b
Expand All @@ -337,7 +339,7 @@ async def wakeup():
loop.create_task(wakeup())

try:
loop.run_until_complete(_share(port, subdomain, remote_host, remote_port))
loop.run_until_complete(_share(port, subdomain, remote_host, remote_port, open))
except KeyboardInterrupt:
tasks = asyncio.all_tasks(loop)
for task in tasks:
Expand All @@ -347,7 +349,7 @@ async def wakeup():
print('Sharing stopped.')


async def _share(port: int, subdomain: str, remote_host: str, remote_port: int):
async def _share(port: int, subdomain: str, remote_host: str, remote_port: int, should_open: bool):
if _scan_free_port(port) == port:
print(f'Could not connect to localhost:{port}. Please make sure your app is running.')
exit(1)
Expand Down Expand Up @@ -381,5 +383,9 @@ async def _share(port: int, subdomain: str, remote_host: str, remote_port: int):
for _ in range(max_conn_count % step):
tasks.append(asyncio.create_task(listen_on_socket('127.0.0.1', port, remote_host, remote_port, share_id)))

if should_open:
await asyncio.sleep(1)
webbrowser.open(remote)

await asyncio.gather(*tasks)
print('Could not establish connection with the server.')

0 comments on commit 08970d8

Please sign in to comment.