Use mosaik with Jupyter Notebooks

You can use mosaik with Jupyter notebooks to have an interactive experience and to write or use tutorials or to document your steps. We have pre-created tutorials written in Jupyter notebooks which you can use. This page gives a short explanation on how to use those.


Starting from version 3.2, mosaik uses asyncio, which does not support nested event loops natively. This leads to an error when using mosaik in a Jupyter notebook. Luckily, the issue can be resolved by installing the library nest_asyncio and calling

import nest_asyncio

at the beginning of your notebook (before creating the mosaik World).

Step 1 - Use Jupyter in VS Code: Jupyter notebooks can be used with different UIs. A simple approach is to use Visual Studio Code with its Jupyter extension. You can find detailed information on how to use Jupyter in VS Code in the VS Code documentation.

Step 2 - mosaik Jupyter repository: Checkout our Jupyter notebook examples repository, e.g. via git clone and open the folder in VS Code.

Step 3 - Virtual Environment: Create a virtual environment with the requirements installed. See the following screenshots for an example on how to create a virtual environment.

Choosing the kernel

Choose the kernel.

Choosing to create a virtual environment

Select the environment type.

Choosing the Python interpreter

Select a python version to create a virtual environment.

Choose to install the requirements

Choose to install the requirements.

Step 4 - Run a Jupyter notebook: Choose one of the available notebooks, e.g., _02_simulator_mosaik.ipynb and open it. You can now run the code blocks step by step or all at once with the “Run All” button on the top. Feel free to play with the example code, extend or change it to your needs or to create your own notebooks based on these examples.