Skip to content

Commit

Permalink
Add example for running from Python
Browse files Browse the repository at this point in the history
  • Loading branch information
visr committed Mar 28, 2024
1 parent af6b1ad commit 085e90e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
7 changes: 1 addition & 6 deletions build/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import platform
import subprocess
from pathlib import Path

Expand All @@ -16,11 +15,7 @@ def test_ribasim_cli(model_constructor, tmp_path):
assert isinstance(model, ribasim.Model)
model.write(tmp_path / "ribasim.toml")

extension = ".exe" if platform.system() == "Windows" else ""

executable = (
Path(__file__).parents[1] / "ribasim_cli" / "bin" / f"ribasim{extension}"
)
executable = Path(__file__).parents[1] / "ribasim_cli" / "bin" / "ribasim"
result = subprocess.run([executable, tmp_path / "ribasim.toml"])

if model_constructor.__name__.startswith("invalid_"):
Expand Down
22 changes: 18 additions & 4 deletions docs/python/examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@
"metadata": {},
"outputs": [],
"source": [
"model.write(datadir / \"basic/ribasim.toml\")"
"toml_path = datadir / \"basic/ribasim.toml\"\n",
"model.write(toml_path)"
]
},
{
Expand All @@ -378,7 +379,7 @@
" \"julia\",\n",
" \"--project=../../core\",\n",
" \"--eval\",\n",
" f'using Ribasim; Ribasim.main(\"{datadir.as_posix()}/basic/ribasim.toml\")',\n",
" f'using Ribasim; Ribasim.main(\"{toml_path.as_posix()}\")',\n",
" ],\n",
" check=True,\n",
")"
Expand All @@ -389,8 +390,21 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Now run the model with `ribasim basic/ribasim.toml`.\n",
"After running the model, read back the results:\n"
"Now run the model. From Python you can run it with:\n",
"\n",
"```python\n",
"import subprocess\n",
"subprocess.call([cli_path, toml_path], check=True)\n",
"```\n",
"\n",
"Windows users should note that if you put the `ribasim_cli` folder in your Path, `cli_path` needs to have the cmd suffix; `ribasim.cmd`.\n",
"\n",
"Or similarly you can from the terminal with:\n",
"```bash\n",
"ribasim basic/ribasim.toml\n",
"```\n",
"\n",
"After running the model, read back the results:"
]
},
{
Expand Down

0 comments on commit 085e90e

Please sign in to comment.