Last active
February 7, 2020 20:14
-
-
Save scj7t4/e804a4bbb6fda5a54589ecd915be54a5 to your computer and use it in GitHub Desktop.
Trying to make it so I can choose stocks with a dynamic map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import holoviews as hv | |
from bokeh.models import CheckboxGroup | |
from bokeh.sampledata.stocks import AAPL, GOOG | |
from holoviews import opts | |
from holoviews.plotting.links import RangeToolLink | |
import pandas as pd | |
from holoviews.streams import Stream | |
hv.extension('bokeh') | |
renderer = hv.renderer('bokeh') | |
aapl_df = pd.DataFrame(AAPL['close'], columns=['close'], index=pd.to_datetime(AAPL['date'])) | |
goog_df = pd.DataFrame(GOOG['close'], columns=['close'], index=pd.to_datetime(GOOG['date'])) | |
aapl_df.index.name = 'Date' | |
goog_df.index.name = 'Date' | |
aapl_curve = hv.Curve(aapl_df, 'Date', ('close', 'Price ($)')) | |
goog_curve = hv.Curve(goog_df, 'Date', ('close', 'Price ($)')) | |
chosen_stocks_stream = Stream.define('Toggle Stocks', active=[0, 1])() | |
def stocks_callback(active, holder_crv): | |
print(f"Callback {active}") | |
src = [('aapl', aapl_curve), ('goog', goog_curve)] | |
d = {k: v for (i, (k, v)) in enumerate(src) if i in active} | |
d['_'] = holder_crv | |
print(f"Plts = {d}") | |
overlay = hv.NdOverlay(d) | |
return overlay | |
def layout_cb(active): | |
# Doing this, because it seems you can't RangeLinkTool with NdOverlay? | |
src_crv = hv.Curve([]) | |
tgt_crv = hv.Curve([]) | |
tgt = stocks_callback(active, tgt_crv).relabel('').opts(width=800, height=400, labelled=['y']) | |
src = stocks_callback(active, src_crv).relabel('').opts(width=800, height=200, yaxis=None, default_tools=[]) | |
RangeToolLink(src_crv, tgt_crv) | |
layout = (tgt + src).cols(1) | |
layout.opts(opts.Layout(shared_axes=False, merge_tools=False)) | |
return layout | |
def change_active_stocks(attr, old, new): | |
layout_cb(active=new) | |
#Comment the other layout line and uncomment this one and the range tool stops working | |
#layout = hv.DynamicMap(layout_cb, streams=[chosen_stocks_stream]) | |
layout = layout_cb([0, 1]) | |
checkbox_group = CheckboxGroup(labels=["AAPL", "GOOG"], active=[0, 1]) | |
checkbox_group.on_change("active", change_active_stocks) | |
renderer.server_doc(layout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment