Skip to content

Commit 80551ae

Browse files
committed
feat: Learned new command "simwrapper open [vsp/asim]"
simwrapper open [site] starts a local file server on port 800x AND opens a new browser tab pointing to it.
1 parent 3501c21 commit 80551ae

5 files changed

Lines changed: 49 additions & 17 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ push: build
3232
> twine upload dist/*
3333
.PHONY: push
3434

35-
bump-version:
35+
version:
3636
> npx standard-version
37-
.PHONY: bump-version
37+
.PHONY: version
3838

3939
#.build-sentinel: $(shell find simwrapper/*.py) $(shell find docs/*) README.md setup.py
4040
.build-sentinel: $(shell find simwrapper/*.py) README.md setup.py

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ We are at the very early stages of building this tool. The API will change, thin
1313
- Our primary goal is to make it easy to get local simulation results viewable using the SimWrapper website.
1414
- We have only tested this using Anaconda Python. Only Python 3.x is supported.
1515

16-
## Quickstart
16+
## Installation
1717

18-
1. Install once, using `pip install simwrapper`
18+
Install once, using `pip install simwrapper`
1919

20-
2. `cd` into your data folder, then run `simwrapper serve` to enable the local file server
20+
## Usage
2121

22-
3. Browse to either <https://vsp.berlin/simwrapper> or <https://activitysim.github.io/dashboard> to view your local folder outputs.
22+
`simwrapper` knows two commands:
23+
24+
- `simwrapper serve` starts a local file server in the current directory. Run this command, then browse to either <https://vsp.berlin/simwrapper> or <https://activitysim.github.io/dashboard> to view your local folder outputs.
25+
26+
- `simwrapper open [vsp|asim]` opens a new web browser tab AND a local file server in the current directory. The site will only operate as long as you keep that local server running, so don't close the command window.
27+
- For the ActivitySim site, use `simwrapper open asim`
28+
- For the VSP MATSim site, use `simwrapper open vsp`

simwrapper/MiniFileServer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ def find_free_port(port):
125125
try:
126126
s.bind(('', i))
127127
s.close()
128-
print("Found free port:",i)
129128
return i
130129

131130
except:
@@ -135,7 +134,8 @@ def find_free_port(port):
135134

136135
def run_mini_file_server(port):
137136
print("\n-----------------------------------------------------------------")
138-
print("SimWrapper file server:", os.getcwd())
137+
print("SimWrapper file server: port", port)
138+
print(os.getcwd())
139139

140140
free_port = find_free_port(port)
141141

simwrapper/cli.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4-
# click makes building cmdline tools easy, see docs at
5-
# https://click.palletsprojects.com/en/8.0.x
4+
import click # cmdline generator! https://click.palletsprojects.com/en/8.0.x
5+
import webbrowser
6+
import os
67

7-
import click
8+
from . import sites
89
from . import MiniFileServer
910

11+
default_port = 8000
12+
1013
@click.group()
1114
def cli():
12-
pass
15+
pass
1316

1417
@cli.command(help="Start a local file server in this folder")
15-
@click.argument('port', default=8000)
18+
@click.argument('port', default=default_port)
1619
def serve(port):
17-
MiniFileServer.run_mini_file_server(port)
20+
MiniFileServer.run_mini_file_server(port)
21+
22+
@cli.command(help="Open your browser and view this folder using SimWrapper")
23+
@click.argument('site', default='vsp')
24+
def open(site):
25+
port = MiniFileServer.find_free_port(default_port)
26+
# Build the full URL for this site, including the free port number
27+
url = ''
28+
if site in sites.sites:
29+
url = os.path.join(sites.sites[site],str(port))
30+
else:
31+
url = os.path.join(site,str(port))
32+
33+
# Open web browser first, because this command returns immediately
34+
print("Opening:", url)
35+
webbrowser.open(url, new=2, autoraise=True) # in a new tab
1836

19-
# @cli.command(help="Open your browser and view files in this folder")
20-
# def browse():
21-
# click.echo("OPEN!!")
37+
# Then start local fileserver
38+
MiniFileServer.run_mini_file_server(port)

simwrapper/sites.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
# Add more SimWrapper sites here:
5+
6+
sites = {
7+
"vsp": "https://vsp.berlin/simwrapper/",
8+
"asim": "https://activitysim.github.io/dashboard/"
9+
}

0 commit comments

Comments
 (0)