How do I share a viewer or a player?¶
In short
- You build: a player file, one HTML page that replays a recorded run in a browser. You also learn when to send a viewer file instead.
- You need: Python 3.11 or later and
pip install nagasim. The reader needs only a browser.- Time: about ten minutes for the player. The viewer needs a 29 MB download once.
A model can leave your machine as one file. By the end you write a player, check what it contains, and choose between it and the viewer for a given reader.
This page has three parts: Step by step, Check yourself and If stuck. The last section lists the reference pages for the elements used.
Step by step¶
- Save this script as
goal-share.py. It builds a one-server queue, records the first replication withnaga run --player, and then opens the file to see what it asks of a browser.
import contextlib
import io
from pathlib import Path
from nagasim import build as nb, cli
# A small model to share: a queue with one server.
m = nb.Model(time_limit=240, warmup=0, replications=3)
m.add_resource(name="Server")
arrive = m.create(distribution=nb.Exponential(rate=1.0))
arrive >> m.process(resource_name="Server", distribution=nb.Exponential(rate=1.25)) >> m.dispose()
m.save("model.json")
# The recording: `naga run` plays the first replication and writes it as one HTML file.
print("$ naga run model.json --player queue.html")
shown = io.StringIO()
with contextlib.redirect_stdout(shown): # the command also prints the KPI table; this page does not need it
code = cli.main(["run", "model.json", "--player", "queue.html"])
print(shown.getvalue().splitlines()[-1])
page = Path("queue.html")
text = page.read_text(encoding="utf-8")
print(f"exit code {code}; {page.name} is {page.stat().st_size / 1000:.0f} KB")
# What the file asks of the browser, and what it leaves out.
print("loads a script from elsewhere:", "<script src" in text)
print("has a content-security-policy:", "Content-Security-Policy" in text)
print("shows the resource name:", "Server" in text)
- Run it.
python goal-share.py
You should see this. The size of the file can change after an engine upgrade.
$ naga run model.json --player queue.html
player written to queue.html
exit code 0; queue.html is 431 KB
loads a script from elsewhere: False
has a content-security-policy: True
shows the resource name: True
-
Open
queue.htmlin a browser. Double-click it. There is no server and no install, and the page makes no request. -
Read the last three lines. The page loads no script from elsewhere, and a Content-Security-Policy makes the browser refuse every request and every script but its own. It shows the resource name, because a reader sees what the model is called.
-
Choose what to send. The player holds one recorded run: names, layout and the sampled run, and no parameter, expression or data source of the model. The reader watches it and cannot change it.
-
Send a viewer when the reader should change inputs and run again. The viewer holds the engine itself, so anyone who gets it can read the whole model. Export it once the runtime is on your machine.
naga viewer --fetch
naga viewer model.json -o model.html
- Pick by the reader and the model. A confidential model goes as a player. A model the client may see in full goes as a viewer. The table below sets them side by side.
| Player | Viewer | |
|---|---|---|
| The reader can | watch one run | change inputs, run again, compare |
| The reader can read | names, layout, the run | the whole model and the engine's source |
| Size | under 10 MB, usually far less | about 35 MB |
| Send it by | a link or a shared drive |
Check yourself¶
- Predict, then run: change
time_limit=240totime_limit=480. Will the file roughly double, or does the engine thin the recording? - Explain it back: why does a viewer, and not a player, need the "whole model" warning?
Done when¶
- [ ]
queue.htmlexists and opens in a browser with no network. - [ ] You can say what a player leaves out of the model.
- [ ] You can say which file to send for a confidential model and which for a client who wants to try inputs.
If stuck¶
| What happens | Likely cause | Fix |
|---|---|---|
| The command fails with a message about the size | The recording passed the 10 MB limit | The engine writes the tape again with fewer samples. If even that does not fit, shorten the run. |
naga viewer says the runtime is missing |
It is downloaded only when you ask | Run naga viewer --fetch, or pass --runtime DIR from another machine. |
| The viewer is refused for a model with data sources | A viewer reads no data source | Add --freeze to write the values read into the copy. |
| The viewer is refused by name | The model has a LOGIC block, a plugin block or agent Python | Share a player instead. A viewer runs no code but the engine's. |
| The page says the browser is not supported | Safari, a phone or a tablet | Use Chrome, Edge or Firefox on a computer. |
Elements this page uses¶
- Blocks: CREATE, PROCESS and DISPOSE.
- Command line:
naga runwith--player, andnaga viewer. - Background: Share a model covers the viewer's levers and the numbers it shows, and Security covers what a shared player carries.
Next: Write a plugin block.