From 085e90e2fb3b7f87e28649254bcecefdb75b9e73 Mon Sep 17 00:00:00 2001 From: Martijn Visser Date: Thu, 28 Mar 2024 17:21:55 +0100 Subject: [PATCH] Add example for running from Python --- build/tests/test_models.py | 7 +------ docs/python/examples.ipynb | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/build/tests/test_models.py b/build/tests/test_models.py index f4d23bb41..61a1aa804 100644 --- a/build/tests/test_models.py +++ b/build/tests/test_models.py @@ -1,4 +1,3 @@ -import platform import subprocess from pathlib import Path @@ -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_"): diff --git a/docs/python/examples.ipynb b/docs/python/examples.ipynb index d2583c8db..4908afb22 100644 --- a/docs/python/examples.ipynb +++ b/docs/python/examples.ipynb @@ -361,7 +361,8 @@ "metadata": {}, "outputs": [], "source": [ - "model.write(datadir / \"basic/ribasim.toml\")" + "toml_path = datadir / \"basic/ribasim.toml\"\n", + "model.write(toml_path)" ] }, { @@ -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", ")" @@ -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:" ] }, {