问题
I am learning bokeh and I am making an application. I ran the directory using the schema given in the documentation bokeh serve --show app
, the code runs successfully and I see the rendering of template and CSS as I expected, but I do not see the plot that I want to generate.
https://github.com/bokeh/bokeh/blob/master/examples/app/weather/main.py
I followed this and also looked on Stackoverflow where it was advised to use curdoc()
, but I still do not see the plot. I am using Python 3.6 and Bokeh 0.13.0 Firefox 61.0 and OS Ubuntu
This is my complete code for main.py
from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.layouts import row
p = figure(title='One sample graph',
plot_width=700,
plot_height=700,
toolbar_location=None)
p.circle([1,2,3,4,5],[6,7,2,5,4], size=15)
curdoc().add_root(row(p))
I also looked at the terminal to see if there were any errors there. It also did not have any error and when I looked at the source code of HTML server, I did not see the code for generating the plot. Am I missing something? Please help.
Edit 1:
I looked at the following tutorial also. This has application without any templates and CSS and doesn't solve my problem.
https://towardsdatascience.com/data-visualization-with-bokeh-in-python-part-iii-a-complete-dashboard-dc6a86aa6e23
Edit 2: Here is the output window of the terminal.
Here is the output when I simply run bokeh serve --show main.py In this case, I only see the plot and lose my template and CSS information. I have also downloaded Chrome to see if there was a problem with the browser, but it is not.
Am I missing something in my code? On gitter, I was informed of using server_document() also, where do I place that statement in my document so that everything gets rendered properly?
回答1:
When running with bokeh serve --show appdir
, templates/index.html
displayed, but the original template did not contain any calls to embed
to specify where plots should go, so no plots showed up. It is necessary to call embed
in the template so that Bokeh knows where to put the plots. The updated template which uses embed
in index.html
looks like so:
main.py:
from bokeh.plotting import figure
from bokeh.io import curdoc
from bokeh.layouts import row
p = figure(title='One sample graph',
plot_width=700,
plot_height=700,
toolbar_location=None)
p.circle([1,2,3,4,5],[6,7,2,5,4], size=15)
curdoc().add_root(row(p, name='plotrow'))
templates/index.html:
{% extends base %}
{% block preamble %}
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://fonts.googleapis.com/css?family=Noto+Sans" rel="stylesheet">
<link rel="stylesheet" href="app/static/special.css">
{% endblock %}
{% block contents %}
<title>Dashboard</title>
<div class="background">
<div class="header">
<h1>Text</h1>
<h2>Some more text</h2>
<p>Even more text</p>
</div>
<div class="bar"></div>
<div class="container">{{ embed(roots.plotrow) }}</div>
<div class="footer">
<p>Some extra text <br/>By someone</p>
</div>
</div>
{% endblock %}
来源:https://stackoverflow.com/questions/51360544/why-am-i-not-seeing-plot-in-bokeh-application-server