Apologies if this question is answered elsewhere on StackOverflow, I did my best to search for an answer before posting this question.
I am working to create a Flask-based web app using Bokeh for data visualization at work. I have had no problem embedding scatter/line plots using bokeh.embed.components and bokeh.plotting.figure.
I would like to display the data which is being used to make the plot in a DataTable widget below the plot. Unfortunately, using components to generate a script and div does not seem to work for widgets. Components also seems to fail when using a column, row, or layout created with bokeh.layouts.
The general layout of my code is a Python file, which contains my Flask app, and an HTML file which contains the layout of my webpage. The general layout of the Python file is below:
from flask import Flask, render_template
import pandas as pd
import numpy as np
from bokeh.models import ColumnDataSource
from bokeh.models.widgets import DataTable, TableColumn
from bokeh.plotting import figure
from bokeh.layouts import widgetbox, column
from bokeh.embed import components
app = Flask(__name__)
x = np.linspace(0, 2, 1000)
y = np.sin(x)
df = pd.DataFrame({"x": x, "y":y}) # Not sure how to provide sample data
@app.route("/")
def index():
p = figure()
p.scatter(df['x'], df['y'])
data_source = ColumnDataSource(df)
columns = [
TableColumn(field="field1", title="Field 1"),
TableColumn(field="field2", title="Field 2"),
TableColumn(field="field3", title="Field 3"),
]
data_table = DataTable(source=data_source, columns=columns)
script, div = components(column(p, widgetbox(data_table)))
return render_template('sample.html', script=script, div=div)
The HTML template ("sample.html") would look something like the following:
{{ script|safe }}
{{ div|safe }}
I've elected to use a simple sine wave as the sample data for this code, but in real life I am using an Excel file (pd.read_excel) to test the code before connecting the web app to a database.
In the Python code, if I replace
script, div = components(column(p, widgetbox(data_table))
with
script, div = components(p)
the code works perfectly. So, that leads me to believe that the problem is with embedding a widget or a layout of plots and widgets. Thanks in advance for any help that you might be able to provide.
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)