In this example we will show you how to integrate our Python code for generating Plotly figures into the Python Code block and use the Plotly Chart block to display it.
It is important to keep in mind that the runtime is not CPython but rather Pyodide that behaves slightly different. For this use case please pay attention at the following points (see the code below):
- for installing the Plotly package please use micropip (see the first 2 lines of code below
- for this example pandas is used behind and it has to be loaded explicitly (please see line 5)
- export the Plotly figure as a JSON-serializable Python Dictionary. Pay attention that we need the corresponding Python Dictionary. So in line 12 we get the JSON string, and we convert it into a Python Dictionary in lines 15-17
Now the variable res can be defined as a JSON output of our Python Code, and used further for the JSON description of the Plotly Chart block.