Plotly with pyscript in Django HTML page

Hello,
I am trying to integrate Plotly with the new method of using Python on an HTML page.

So I am trying to run this example:

<html>
  <head>

    <title>plotly.express</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>

    <py-env>
      - plotly-express
    </py-env>
    </head>
    <body>

    <center><h3>Wait it took long time to load</h3></center>
    <div id="mpl"></div>
      <py-script output="mpl">
import plotly.express as px
df = px.data.gapminder().query("year == 2007").query("continent == 'Europe'")
df.loc[df['pop'] < 2.e6, 'country'] = 'Other countries' # Represent only large countries
fig = px.pie(df, values='pop', names='country', title='Population of European continent')

fig.show()
      </py-script>
    </body>
</html>

After a few minutes :frowning: I get an error !

JsException(PythonError: Traceback (most recent call last): File "/lib/python3.10/site-packages/plotly/io/_renderers.py", line 532, in webbrowser.get() AttributeError: module 'webbrowser' has no attribute 'get' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/lib/python3.10/site-packages/_pyodide/_base.py", line 429, in eval_code .run(globals, locals) File "/lib/python3.10/site-packages/_pyodide/_base.py", line 300, in run coroutine = eval(self.code, globals, locals) File "", line 6, in File "/lib/python3.10/site-packages/plotly/basedatatypes.py", line 3400, in show return pio.show(self, *args, **kwargs) File "/lib/python3.10/site-packages/_plotly_utils/importers.py", line 36, in __getattr__ class_module = importlib.import_module(rel_module, parent_name) File "/lib/python3.10/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1050, in _gcd_import File "", line 1027, in _find_and_load File "", line 1006, in _find_and_load_unlocked File "", line 688, in _load_unlocked File "", line 883, in exec_module File "", line 241, in _call_with_frames_removed File "/lib/python3.10/site-packages/plotly/io/_renderers.py", line 534, in except webbrowser.Error: AttributeError: module 'webbrowser' has no attribute 'Error' )

Hi,
Take a look here.
In example 2 it is said that pyscript doesn’t support Plotly for now (the guy use Brokeh because of that)
I hope that help you

1 Like