Hi,
I’m trying to use Bokeh Server to launch via TapTool a new browser tab with html content (eventually will be xml content)? Basically nothing happens - no errors, no new tab.
I’ve simplified what I’m doing down to the example below. Opening a URL works fine but cannot get the html content displayed? I’ve limited knowledge of JavaScript (one of my reasons for my interest in Bokeh). Basic mistake by me or something more fundamental?
Example modified from here:interaction_open_url.py — Bokeh 1.0.0 documentation
And ideas on the JavaScript part mainly from here: javascript - Can I open a new window and populate it with a string variable? - Stack Overflow
Thanks in advance,
Dave
from bokeh.server.server import Server
from bokeh.models import CustomJS, ColumnDataSource, TapTool
from bokeh.plotting import figure
html_content = """
<HTML>
<HEAD>
<TITLE>Your Title Here</TITLE>
</HEAD>
<BODY BGCOLOR="FFFFFF">
<CENTER><IMG SRC="clouds.jpg" ALIGN="BOTTOM"> </CENTER>
<HR>
<a href="http://somegreatsite.com">Link Name</a>
is a link to another nifty site
<H1>This is a Header</H1>
<H2>This is a Medium Header</H2>
Send me mail at <a href="mailto:[email protected]">
[email protected]</a>.
<P> This is a new paragraph!
<P> <B>This is a new paragraph!</B>
<BR> <B><I>This is a new sentence without a paragraph break, in bold italics.</I></B>
<HR>
</BODY>
</HTML>
"""
def modify_doc(doc):
p = figure(plot_width=400, plot_height=400, title="Click the Dots")
source = ColumnDataSource(data=dict(
x=[1, 2, 3, 4, 5],
y=[2, 5, 8, 2, 7],
color=["navy", "orange", "olive", "firebrick", "gold"]
))
p.circle('x', 'y', color='color', size=20, source=source)
tap_tool = TapTool()
tap_tool.behavior = 'select'
# This works fine.
# js_code = """
# window.open("http://www.bbc.com");
# """
# Does not work - no new tab.
js_code = """
var wnd = window.open("about:blank", "", "_blank")
wnd.document.write({0})
""".format(html_content)
# Does not work - no new tab.
# js_code = """
# window.open("data:text/html;charset=utf-8,"+{0}, "", "_blank")
# """.format(html_content)
# Does not work - no new tab.
# js_code = """
# var wnd = window.open("data:text/html;charset=utf-8,", "", "_blank");
# wnd.document.write({0});
# wnd.document.close();
# """.format(html_content)
tap_tool.callback = CustomJS(code=js_code)
p.add_tools(tap_tool)
# Add the tabs in the current document for display
doc.add_root(p)
# Setting num_procs here means we can't touch the IOLoop before now, we must
# let Server handle that. If you need to explicitly handle IOLoops then you
# will need to use the lower level BaseServer class.
server = Server({'/': modify_doc},
num_procs=1) # note: on Windows more than one process not supported
if __name__ == '__main__':
server.io_loop.add_callback(server.show, "/")
server.io_loop.start()