Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add argument for automatic TCP tunneling #71

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion examples/00_coordinate_frames.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import viser

server = viser.ViserServer()
server = viser.ViserServer(share=True)

while True:
# Add some coordinate frames to the scene. These will be visualized in the viewer.
Expand Down
2 changes: 1 addition & 1 deletion examples/08_smplx_visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def main(
num_expression_coeffs: int = 10,
ext: Literal["npz", "pkl"] = "npz",
) -> None:
server = viser.ViserServer()
server = viser.ViserServer(share=True)
model = smplx.create(
model_path=str(model_path),
model_type=model_type,
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies = [
"rich>=13.3.3",
"trimesh>=3.21.7",
"nodeenv>=1.8.0",
"rustenv>=0.0.6",
]

[project.optional-dependencies]
Expand Down
61 changes: 61 additions & 0 deletions viser/_tunnel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""Helper for creating shareable viser links using `bore`.

https://github.com/ekzhang/bore
"""

import atexit
import os
import signal
import subprocess
import sys
import threading
from pathlib import Path

BORE_SERVER = "chalupa.eecs.berkeley.edu"


def start_tunnel(port: int) -> None:
env_dir = Path(__file__).absolute().parent / ".rustenv"
bore_path = env_dir / "rust" / "bin" / "bore"

if not env_dir.exists():
print("[viser] Tunneling: setting up Rust environment")
subprocess.run(args=[sys.executable, "-m", "rustenv", str(env_dir)])

if not bore_path.exists():
print("[viser] Tunneling: installing bore")
subprocess.run(args=[str(env_dir / "bin" / "cargo"), "install", "bore-cli"])

process = subprocess.Popen(
[f"{bore_path}", "local", str(port), "--to", BORE_SERVER],
stdout=subprocess.PIPE,
)

# Handle normal exists.
@atexit.register
def _():
process.terminate()
process.wait()

# Handle SIGTERM.
script_pid = os.getpid()

def handle_termination_signal(signum, frame):
process.kill()
os.kill(script_pid, signum)

signal.signal(signal.SIGTERM, handle_termination_signal)

def _bore_watcher() -> None:
while True:
assert process.stdout is not None
line = process.stdout.readline().decode().strip()
if "Error:" in line:
print(line)
break
if "listening at " in line:
print("[viser] Tunnel created at", line.partition("listening at ")[2])

threading.Thread(target=_bore_watcher).start()

return None
6 changes: 5 additions & 1 deletion viser/_viser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from ._gui_api import GuiApi
from ._message_api import MessageApi, cast_vector
from ._scene_handles import FrameHandle, _SceneNodeHandleState
from ._tunnel import start_tunnel


@dataclasses.dataclass
Expand Down Expand Up @@ -229,7 +230,10 @@ class ViserServer(MessageApi, GuiApi):
"""Handle for manipulating the world frame axes (/WorldAxes), which is instantiated
and then hidden by default."""

def __init__(self, host: str = "0.0.0.0", port: int = 8080):
def __init__(self, host: str = "0.0.0.0", port: int = 8080, share: bool = False):
if share:
start_tunnel(port)

server = infra.Server(
host=host,
port=port,
Expand Down
Loading