Skip to content

Commit 1c7ccef

Browse files
Improve documentation and clarify preferences
1 parent 609b858 commit 1c7ccef

3 files changed

Lines changed: 141 additions & 25 deletions

File tree

README.md

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,52 @@
11
# Brian2Wasm
22

3-
Wasm code generation for the [Brian simulator](https://briansimulator.org).
3+
**Brian2Wasm** is a [Brian 2](https://briansimulator.org/) “device” that compiles Brian models to WebAssembly and JavaScript via the [Emscripten](https://emscripten.org/) tool-chain.
4+
The result is a self-contained web folder (`index.html`, `wasm_module.js`, and the `.wasm` binary) that runs your simulation in any modern browser.
45

5-
Using the [emscripten](https://emscripten.org/) toolchain, this "device" generates code in WebAssembly and JavaScript, enabling users to run Brian simulations in the browser.
6+
Live examples: <https://brian-team.github.io/brian2wasm/>
67

7-
You can directly access the built examples on this website: https://brian-team.github.io/brian2wasm/
8+
> **Status** – functional, but still under active development.
89
9-
**UNDER CONSTRUCTION, not for general use**
10+
[![PyPI](https://img.shields.io/pypi/v/brian2wasm.svg)](https://pypi.org/project/brian2wasm/)
1011

11-
> [!WARNING]
12-
> The package currently only targets Linux – it might work on macOS and on Windows via the WSL, but this hasn't been tested.
12+
## Installation & Usage (Pixi)
1313

14-
Usage/Installation:
15-
- Install `brian2wasm` with `pip`, either by cloning this repository and using `pip install .`, or by using
16-
```console
17-
$ pip install git+https://github.com/brian-team/brian2wasm/
18-
```
19-
- Install the [emsdk](https://emscripten.org/docs/getting_started/downloads.html) and activate it following the instructions
20-
- You should then be able to run one of the examples in the [`examples` folder](/examples)
14+
```bash
15+
# 1 – install Pixi (https://pixi.sh)
16+
curl -fsSL https://pixi.sh/install.sh | bash
2117

22-
> [!NOTE]
23-
> Plotting will not work in the website that gets started automatically by the example script, since it needs to download the `plotly.js` library from a CDN.
24-
> You can work around this limitation by going into the generated folder that contains the `index.html` file and run a Python webserver via `python -m http.server`. You can then open the displayed link in your browser.
18+
# 2 – set up Brian2Wasm
19+
git clone https://github.com/brian-team/brian2wasm.git
20+
cd brian2wasm
21+
pixi install # creates the full env (Python, emsdk, brian2, …)
22+
pixi run setup # one-time EMSDK activation
23+
pixi shell # enter the environment shell
24+
25+
# 3 – build and run an example
26+
python -m brian2wasm examples/brunel_hakim1999.py
27+
```
28+
29+
## Headless build (no preview server)
30+
```bash
31+
python -m brian2wasm --no-server my_model.py
32+
```
33+
```--no-server``` skips the temporary web-server and only generates the files.
34+
35+
36+
> **⚠️ Limitations / Warnings**
37+
> * **Do not call `set_device(...)`** in your script—Brian2Wasm sets the device automatically.
38+
> * Plotly charts inside the generated HTML load assets from a CDN.
39+
40+
41+
## Contributing
42+
43+
Contributions are welcome!
44+
If you encounter a bug or have a feature request, please open an issue first.
45+
Pull requests should target the `main` branch and follow conventional commit messages.
46+
47+
---
48+
49+
## License
50+
51+
Brian2Wasm is released under the same open-source license as the core Brian 2 simulator (BSD-style).
52+
See the `LICENSE` file in this repository for full details.

brian2wasm/__main__.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,45 @@
33
import os
44

55
def main():
6+
"""
7+
Command-line interface for **Brian2Wasm**.
8+
9+
Usage
10+
-----
11+
``python -m brian2wasm <script.py> [--no-server]``
12+
13+
Parameters
14+
----------
15+
script : str
16+
Path to the user’s Python model. The file **must** end with
17+
``.py`` and must not call ``set_device`` itself – the CLI inserts
18+
the appropriate ``set_device('wasm_standalone', …)`` line
19+
automatically.
20+
--no-server : flag, optional
21+
Generate the WASM/HTML output without starting the local preview
22+
web-server (sets the ``BRIAN2WASM_NO_SERVER`` environment
23+
variable for the subprocess).
24+
25+
Behaviour
26+
---------
27+
1. Validates that *script* exists and is a ``.py`` file.
28+
2. Looks for an ``<scriptname>.html`` file in the same directory.
29+
* If found, passes the HTML file to ``set_device`` so the custom
30+
template is used.
31+
* Otherwise falls back to the default template.
32+
3. Prepends the required ``set_device('wasm_standalone', …)`` call to
33+
the script source in-memory.
34+
4. Executes the modified script with its own directory as working
35+
directory, so any relative paths inside the model behave as
36+
expected.
37+
38+
Exit status
39+
-----------
40+
* ``0`` – build finished successfully (and server started unless
41+
*--no-server* was given).
42+
* ``1`` – any error (missing file, not a ``.py`` file, exception
43+
during model execution, etc.).
44+
"""
645

746
parser = argparse.ArgumentParser(
847
description="Brian2WASM CLI"

brian2wasm/device.py

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,34 @@
2929
'devices.wasm_standalone',
3030
'Preferences for the WebAsm backend',
3131
emsdk_directory=BrianPreference(
32-
docs='''Path to the emsdk directory, containing the emsdk binary.''',
33-
default=''
32+
default="",
33+
docs="""
34+
Absolute path to the *emsdk* installation. Leave empty to use the
35+
EMSDK/CONDA_EMSDK_DIR environment variables or an already-activated
36+
emsdk in your shell.
37+
""",
3438
),
3539
emsdk_version=BrianPreference(
36-
docs='''Version of the emsdk to use, defaults to "latest"''',
37-
default="latest"
40+
default="latest",
41+
docs="""
42+
Version string passed to ``emsdk activate`` (e.g. ``"3.1.56"``).
43+
Ignored when *emsdk_directory* is empty and the SDK is pre-activated.
44+
""",
3845
),
3946
emcc_compile_args=BrianPreference(
40-
default=[
41-
"-w"
42-
],
43-
docs="Extra flags appended to every emcc compile command",
47+
default=["-w"],
48+
docs="""
49+
Extra flags appended to every *emcc* **compile** command.
50+
Example: ``["-O3", "-sASSERTIONS"]``.
51+
""",
4452
),
4553
emcc_link_args=BrianPreference(
4654
default=[],
47-
docs="Extra flags passed at link time",
55+
docs="""
56+
Extra flags appended to the final *emcc* **link** command that produces
57+
``wasm_module.js`` / ``.wasm``.
58+
Example: ``["-sEXPORT_ES6", "-sEXPORTED_RUNTIME_METHODS=['cwrap']"]``.
59+
""",
4860
),
4961
)
5062

@@ -397,6 +409,43 @@ def run(self, directory, results_directory, with_output, run_args):
397409
self.timers['run_binary'] = time.time() - start_time
398410

399411
def build(self, html_file=None, html_content=None, **kwds):
412+
"""
413+
Build the project for the WASM backend.
414+
415+
Parameters
416+
----------
417+
directory : str, optional
418+
Target folder for the generated project. If ``None`` a temporary
419+
directory is created. Default: ``"output"``.
420+
results_directory : str, optional
421+
Relative sub-folder used at runtime to store simulation results.
422+
Default: ``"results"``.
423+
compile : bool, optional
424+
Compile the generated sources with *emcc*. Default: ``True``.
425+
run : bool, optional
426+
Execute the produced JavaScript/WASM bundle after a successful
427+
build (headless node-style run). Default: ``True``.
428+
debug : bool, optional
429+
Add debug symbols and ``-g -DDEBUG`` flags. Default: ``False``.
430+
clean : bool, optional
431+
Remove previously compiled objects before building. Default:
432+
``False``.
433+
with_output : bool, optional
434+
Forward the program’s stdout/stderr when running. Default:
435+
``True``.
436+
additional_source_files : list[str] | None
437+
Extra ``.cpp`` files to compile alongside the generated Brian code.
438+
run_args : list[str] | None
439+
Additional command-line arguments passed to the executable HTML/
440+
JS harness when *run* is ``True``.
441+
direct_call : bool, optional
442+
``True`` when invoked by user code, ``False`` when invoked
443+
automatically because ``build_on_run=True``.
444+
**kwds
445+
Reserved for future keyword arguments; passing unknown names
446+
raises ``TypeError``.
447+
"""
448+
400449
self.build_options.update({'html_file': html_file,
401450
'html_content': html_content})
402451

0 commit comments

Comments
 (0)