pywebio / PyWebIO

Write interactive web app in script way.
https://pywebio.readthedocs.io
MIT License
4.54k stars 383 forks source link

Jupyter Notebook Data Visualization Returns Blank #100

Closed shakurgds closed 3 years ago

shakurgds commented 3 years ago

BUG Description

Running the following code to create Plotly Data Visualization inside Jupyter Notebook returns a blank page:

from pywebio.output import put_html
from urllib.request import urlopen
import json
with urlopen('https://raw.githubusercontent.com/plotly/datasets/master/geojson-counties-fips.json') as response:
    counties = json.load(response)

import pandas as pd
df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/fips-unemp-16.csv",
                   dtype={"fips": str})

import plotly.express as px

fig = px.choropleth_mapbox(df, geojson=counties, locations='fips', color='unemp',
                           color_continuous_scale="Viridis",
                           range_color=(0, 12),
                           mapbox_style="carto-positron",
                           zoom=3, center = {"lat": 37.0902, "lon": -95.7129},
                           opacity=0.5,
                           labels={'unemp':'unemployment rate'}
                          )
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
html = fig.to_html(include_plotlyjs="require", full_html=False)
put_html(html)`

Environment Information

wang0618 commented 3 years ago

If you use PyWebIO in interactive execution environment of Python shell, IPython or jupyter notebook, you need call send() method explicitly to show output:

>>> put_text("Hello world!").send()
>>> put_table([
...     ['A', 'B'],
...     [put_markdown(...), put_text('C')]
... ]).send()

I also added this note to the document in https://github.com/wang0618/PyWebIO/commit/6c634b497238dd71d3a49f654eca5a3f7d7e5f88 , the updates will be visible in the next version.

shakurgds commented 3 years ago

It works now. Great!

Shahin-rmz commented 3 years ago

If you use PyWebIO in interactive execution environment of Python shell, IPython or jupyter notebook, you need call send() method explicitly to show output:

>>> put_text("Hello world!").send()
>>> put_table([
...     ['A', 'B'],
...     [put_markdown(...), put_text('C')]
... ]).send()

I also added this note to the document in 6c634b4 , the updates will be visible in the next version.

This means for deploying on the server, we have to delete .send attributes in order to be able to execute the files as .py files?

wang0618 commented 3 years ago

Remove .send() call is not necessary when deploying on the server. But this may be confusing for otherone who don't konw this feature.