Chart Pandas DataFrame columns using Holoviews DynamicMap served via Flask

梦想与她 提交于 2019-12-10 11:20:56

问题


Following this tutorial, I am trying to visualise a dataset using Holoviews instead of Bokeh (sample data available here as a CSV file), serving the results using Flask. I decided to use Flask and not Bokeh Server because I am building a larger workflow using the former.

My code is the following:

from flask import Flask, render_template, request
import numpy as np
import pandas as pd
from datetime import datetime
from bokeh.embed import components
from bokeh.io import curdoc
import holoviews as hv
hv.extension("bokeh")

app = Flask(__name__)

renderer = hv.renderer('bokeh')
infile = "./uploads/test.csv"

def loadRegionData(regionProperty, **kwargs):
    df = pd.read_csv(infile, parse_dates=['Datetime'])
    df1 = df[regionProperty]
    df = pd.concat([df['Datetime'],df1], axis=1)

    return hv.Curve(df)

colNames = ((pd.read_csv(infile, nrows=1)).drop(['Datetime'], axis=1)).columns.values

dmap = hv.DynamicMap(loadRegionData, kdims='RegionProperty').redim.values(RegionProperty=colNames)

hvplot = renderer.get_plot(dmap)
plot = hvplot.state
plot.name = 'plot'
curdoc().add_root(plot)

@app.route("/")
def index():
    # Embed plot into HTML via Flask Render
    script, div = components(plot)
    return render_template("index.html", script=script, div=div)

if __name__ == '__main__':
    app.run(port=5000, debug=True)

I am running into the following (unrelated issues)

  1. When I deploy using Flask, the dropdowns to select the columns do not appear. I suspect that is because I am not returning/referring to the correct variables from the index() function into my index.html:

<html>
<head>
    <link
        href="http://cdn.bokeh.org/bokeh/release/bokeh-1.0.2.min.css"
        rel="stylesheet" type="text/css">
    <link
        href="http://cdn.bokeh.org/bokeh/release/bokeh-widgets-1.0.2.min.css"
        rel="stylesheet" type="text/css">

    <script src="http://cdn.bokeh.org/bokeh/release/bokeh-1.0.2.min.js"></script>
    <script src="http://cdn.bokeh.org/bokeh/release/bokeh-widgets-1.0.2.min.js"></script>

</head>
<body>
    <h1>Holoview test</h1>
    {{ script|safe }}
    {{ div|safe }}
</body>
</html>

How can I get Flask to also show the dropdown selector?

  1. An unrelated issue which I found when I tested this app using Bokeh Server, and which could also arise in the Flask implementation, is that the scales do not adjust dynamically based on my column selection. Perhaps this can go as a separate question on SO, but I thought to include it here for now to keep things together.

来源:https://stackoverflow.com/questions/53703731/chart-pandas-dataframe-columns-using-holoviews-dynamicmap-served-via-flask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!