holoviz / holoviews

With Holoviews, your data visualizes itself.
https://holoviews.org
BSD 3-Clause "New" or "Revised" License
2.7k stars 404 forks source link

using holoviews and bokeh to generate html file without using iPython Notebook #1819

Closed LuckyGem closed 7 years ago

LuckyGem commented 7 years ago

I have a question about using holoviews and bokeh together to generate html file without using iPython Notebook. I have used bokeh before to generate interactive html file but I am not sure how to do it with holoviews. I want to have a time series graph and a dropdown menu(for example sales of fruit across year, the drop down menu will contain the different kind of frute), the graph will change according to fruite type. For example, if I want to have a time series graph, with bokeh alone we can plot=TimeSeries(data,index='Date',title="fruit X",ylabel='sales') output_file("graph.html") show(plot) I wonder if I can do something similar without using iPython Notebook? Really appreciate you help, thank you.

philippjfr commented 7 years ago

Sorry for the delay, we really need to document this better. There are two ways of saving out data:

curve = hv.Curve(range(10)) # Could be any HoloViews object

renderer = hv.renderer('bokeh')

# Using renderer save
renderer.save(curve, 'graph.html')

# Convert to bokeh figure then save using bokeh
plot = renderer.get_plot(curve).state

from bokeh.io import output_file, save, show
save(plot, 'graph.html')
# OR
output_file("graph.html")
show(plot)
Sieboldianus commented 7 years ago

Wow, this should really be added to the Holoviews Userguide here! Thanks so much for explanation.

hygull commented 6 years ago

@philippjfr ,You gave an excellent answer. It helped me a lot. Thanks.

Note: Only specify html file name without an extension otherwise file will be saved as graph.html.html.

eg.

curve = hv.Curve(range(10)) # Could be any HoloViews object

renderer = hv.renderer('bokeh')

# Using renderer save
renderer.save(curve, 'graph')
elmbeech commented 6 years ago

Hi @philippjfr - this is semi related.

The explanation was really helpful to me! I tried to use the same technique to store a hv.Scatter object using plotly as backed. (Because there is a sudden feature on the ploty scatter plot I prefer over the bokeh one.)

This is my attempt and observation:

# load libraries
import pandas as pd
import plotly
import holoviews as hv
hv.extension('plotly')

# generate holoviews scatter object
df = pd.DataFrame([[1,2,3],[4,5,6]]).T
df.columns = ['x', 'y']
obj = hv.Scatter(df, 'x', 'y')

First I tried:

hv.renderer('plotly').save(obj, 'output_1')

This generates a output_1.html file, but opened it in a webbrowser only shows a Drawing... webpage forever (png attached).

output_1b

Then I tried:

objstate = hv.renderer('plotly').get_plot(obj).state
plotly.offline.plot(objstate, filename='output_2.html')

This generated a output_2.html file, which worked! (png attached).

output_2b

I know that the plotly backed is beta and not fully supported yet. But It would be super cool if the first method would work too.

I just detected holoviews (and hvplot) lately. I am still quite noob to it and I have no idea where to start hacking in the code base to get this working. Maybe you can point me to the right part in the source code? Or otherwise I let it for the moment as a "improvement suggestion". Anyhow, till now I am really impressed with what is achievable with holoviews and hvplot.

Thank you very much, Elmar

zxdawn commented 5 years ago

Is it possible to convert time_series plot to html file?

Here's an example: No matter how I drug the time bar, the picture is same.

import xarray as xr
import hvplot
import hvplot.pandas
import hvplot.xarray
import geoviews as gv
import cartopy.crs as ccrs
import holoviews as hv

air_ds = xr.tutorial.open_dataset('air_temperature').load()

figure = air_ds.hvplot.quadmesh(
    'lon', 'lat', 'air',projection=ccrs.PlateCarree(),cmap='viridis'
) * gv.feature.coastline * gv.feature.borders

handle = display(figure)

renderer = hv.renderer('bokeh')
renderer.save(figure, 'graph')
philippjfr commented 5 years ago

Note dynamic=False, also we have a top-level save function now:

import xarray as xr
import hvplot
import hvplot.pandas
import hvplot.xarray
import geoviews as gv
import cartopy.crs as ccrs
import holoviews as hv

air_ds = xr.tutorial.open_dataset('air_temperature').load()

figure = air_ds.hvplot.quadmesh(
    'lon', 'lat', 'air',projection=ccrs.PlateCarree(),cmap='viridis', dynamic=False
) * gv.feature.coastline * gv.feature.borders

hv.save(figure, 'graph.html')
zxdawn commented 5 years ago

@philippjfr Thanks! It works now 👍

LiShi2269 commented 5 years ago

Note dynamic=False, also we have a top-level save function now:

import xarray as xr
import hvplot
import hvplot.pandas
import hvplot.xarray
import geoviews as gv
import cartopy.crs as ccrs
import holoviews as hv

air_ds = xr.tutorial.open_dataset('air_temperature').load()

figure = air_ds.hvplot.quadmesh(
    'lon', 'lat', 'air',projection=ccrs.PlateCarree(),cmap='viridis', dynamic=False
) * gv.feature.coastline * gv.feature.borders

hv.save(figure, 'graph.html')

do you have a top-level show function now?

ahuang11 commented 5 years ago

There's hvplot.show()

https://github.com/pyviz/hvplot/blob/f041c812a5ab078ad4e8dfbfa952ea8330f5dd0f/hvplot/utilities.py#L47

stevenlis commented 5 years ago

I'm having trouble to save a linked plot as an html or some file format that I can simply share with others who don't use jupyter notebook. Here's the example from the doc.

import numpy as np
import holoviews as hv
hv.extension('bokeh')

# Declare some points
points = hv.Points(np.random.randn(1000,2 )).opts(tools=['box_select', 'lasso_select'])

# Declare points as source of selection stream
selection = hv.streams.Selection1D(source=points)

# Write function that uses the selection indices to slice points and compute stats
def selected_info(index):
    arr = points.array()[index]
    if index:
        label = 'Mean x, y: %.3f, %.3f' % tuple(arr.mean(axis=0))
    else:
        label = 'No selection'
    return points.clone(arr, label=label).opts(color='red')

# Combine points and DynamicMap
selected_points = hv.DynamicMap(selected_info, streams=[selection])

layout = points + selected_points

Is there any way to save the layout together while the interactivity still working? So far, I have only figured out how to make a bokeh.application.

ahuang11 commented 5 years ago

DynamicMaps require a backend server, useful if you want to do computations on the go.

Since you're taking a mean of a selection of points, you can't export it as an HTML because there's additional computations to be made.

If you simply want a linked plot where you can select points and the corresponding points in other subplots get highlighted:

image

import holoviews as hv

hv.extension('bokeh')

df = pd.DataFrame({'x': [0, 1, 2], 'y': [4, 5, 6], 'z': [5, 7, 9]})

linked_plots = (
    (hv.Scatter(df, 'x', 'y') + hv.Scatter(df, 'x', 'z') + hv.Table(df))
    .opts('Scatter', tools=['lasso_select'], padding=0.5)
    .opts(shared_datasource=True)
)
hv.save(linked_plots, 'linked_plots.html')
Muralitharan2789 commented 4 years ago

I have did the same as mentioned above, html file is saving and opening while trying from google colab.

But html saved while running the same code in Pycharm is not opening. Please share any idea or answer for the same. I have struck with this for an day

Muralitharan2789 commented 4 years ago

Its work with the latest version, If we have to use Python 3.6, bokeh version 2.11 and holoviews latest version worked for my work.

Keramatfar commented 3 years ago

Sorry for the delay, we really need to document this better. There are two ways of saving out data:

curve = hv.Curve(range(10)) # Could be any HoloViews object

renderer = hv.renderer('bokeh')

# Using renderer save
renderer.save(curve, 'graph.html')

# Convert to bokeh figure then save using bokeh
plot = renderer.get_plot(curve).state

from bokeh.io import output_file, save, show
save(plot, 'graph.html')
# OR
output_file("graph.html")
show(plot)

Thanks, it works but remove styles.

github-actions[bot] commented 2 weeks ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.