Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed "Bokeh created html file" into Flask "template.html" file

I have a web application written in Python - Flask. When the user fill out some settings in one of the pages (POST Request), my controller calculates some functions and plot an output using Bokeh with following command and then I redirect to that HTML page created by Bokeh.

output_file("templates\\" + idx[j]['name'] + ".html", title = "line plots")
TOOLS="resize,crosshair,pan,wheel_zoom,box_zoom,reset,box_select,lasso_select"
p = figure(tools=TOOLS, x_axis_label = 'time', y_axis_label = 'L', plot_width = 1400, plot_height = 900)

All of my HTML pages extends my "Template.HTML" file except the Bokeh generated ones. My question is how can automatically modify Bokeh generated HTML files to also extends my template.html file? This way I have all my nav-bar & jumbotron on top of the Bokeh html files.

  {% extends "template.html" %}
  {% block content %}

  <Bokeh.html file>

  {% endblock %}
like image 462
Hamid K Avatar asked Oct 28 '15 18:10

Hamid K


People also ask

How do I install a template in flask?

html template file in a directory called templates inside your flask_app directory. Flask looks for templates in the templates directory, which is called templates , so the name is important. Make sure you're inside the flask_app directory and run the following command to create the templates directory: mkdir templates.

How do you write an HTML flask in Python?

Flask is one of the web development frameworks written in Python. Through flask, a loop can be run in the HTML code using jinja template and automatically HTML code can be generated using this. The code will be stored in Directories in the format of Flask.


1 Answers

You don't want to use output_file in this situation. Bokeh has a function specifically for embedding into HTML templates in web apps, bokeh.embed.component, demonstrated in the quickstart and tutorial.

from bokeh.embed import components
script, div = components(plot)
return render_template('page.html', script=script, div=div)
<body>
{{ div|safe }}
{{ script|safe }}
</body>

Here is a complete, runnable example that that shows how to use this with Flask.

like image 161
bigreddot Avatar answered Oct 06 '22 19:10

bigreddot