Python Bokeh服务器与Flask错误的集成

Python Bokeh服务器与Flask错误的集成,python,google-maps,flask,bokeh,Python,Google Maps,Flask,Bokeh,我正试图将我的bokeh服务器应用程序与Flask集成在一起。我正在使用bokeh的GMapOptions库生成一个可视化,显示我的客户数据在一个地理区域内的分布。我运行Flask应用程序代码时出现以下错误 TypeError: __init__() missing 2 required positional arguments: 'google_api_key' and 'map_options' 当我在命令提示符下使用bokeh serve命令独立运行bokeh服务器应用程序时,我在命令

我正试图将我的bokeh服务器应用程序与Flask集成在一起。我正在使用bokeh的GMapOptions库生成一个可视化,显示我的客户数据在一个地理区域内的分布。我运行Flask应用程序代码时出现以下错误

TypeError: __init__() missing 2 required positional arguments: 'google_api_key' and 'map_options' 
当我在命令提示符下使用bokeh serve命令独立运行bokeh服务器应用程序时,我在命令提示符中得到了相同的消息,但应用程序仍然运行。显然,生成消息的是bokeh库中的某种缺陷,但据库的开发人员称,这不应阻止我的应用程序运行,而独立运行时不会。我向库的开发人员报告了这个bug

这是我的可视化代码

from bokeh.plotting import output_file
from bokeh.plotting import gmap
from bokeh.io import curdoc
from bokeh.models import Button, GMapOptions, ColumnDataSource, CategoricalColorMapper
from bokeh.models import *
from bokeh.layouts import column, row
import pandas as pd
import numpy as np
import pymssql



# parameters
server = 'X.X.X.X\PQR'
db = 'ABC'

# Create the connection
conn = pymssql.connect(database = db, host = server)
cur = conn.cursor()
map_options = GMapOptions(lat=37.686293, lng=-97.3614409, map_type="roadmap", zoom=13)
p = gmap("My Google Maps API Key", map_options, title="Resolutions Clients", plot_width=1000, plot_height=600)

sql = """
        with CTE AS
            (SELECT
            CR.ClientID, CR.Latitude, CR.Longtitude
            FROM 
            Company C LEFT JOIN ClientRegistration CR on C.CompanyID = CR.CompanyID
            WHERE
            C.CompanyID = 555 AND (CR.Latitude IS NOT NULL AND CR.Longtitude IS NOT NULL)
            )
        SELECT
        C.ClientID, C.Latitude, C.Longtitude, CL.Sex
        FROM
        CTE C LEFT JOIN Clients CL on C.ClientID = CL.ClientID
    """

df = pd.read_sql(sql, conn)



df[['Latitude', 'Longtitude']] = df[['Latitude', 'Longtitude']].apply(pd.to_numeric)
df['Sex'] = df['Sex'].astype('str')

map_options = GMapOptions(lat=37.686293, lng=-97.3614409, map_type="roadmap", zoom=10)
p = gmap("My Google Maps API Key", map_options, title="Resolutions Clients Genderwise", plot_width=1000,
             plot_height=600)

lat = df['Latitude'].tolist()
lon = df['Longtitude'].tolist()
sex = df['Sex'].tolist()

source = ColumnDataSource(
        data=dict(latitude=lat,
                  longitude=lon,
                  gender=sex
                  )
)


color_mapper = CategoricalColorMapper(factors=['M', 'F', 'U'], palette=['Red', 'Blue', 'Green'])


p.circle(x="longitude", y="latitude", size=4, fill_alpha=0.9, source=source,
             fill_color={'field': 'gender', 'transform': color_mapper},
             line_color={'field': 'gender', 'transform': color_mapper})

x = np.linspace(0, 4 * np.pi, 100)
y = np.sin(x)

p.circle(x, y, legend="Male", color="red")
p.circle(x, 2 * y, legend="Female", color="Blue")
p.circle(x, 3 * y, legend="Unknown", color="Green")

def update():
    # query db
    curdoc().clear()
    sql = """
            with CTE AS
                (SELECT
                CR.ClientID, CR.Latitude, CR.Longtitude
                FROM
                Company C LEFT JOIN ClientRegistration CR on C.CompanyID = CR.CompanyID
                WHERE
                C.CompanyID = 555 AND (CR.Latitude IS NOT NULL AND CR.Longtitude IS NOT NULL)
                )
            SELECT
                C.ClientID, C.Latitude, C.Longtitude,
                CASE WHEN age > 0 AND age <= 15 THEN 'Children'
                     WHEN age > 15 AND age <= 30 THEN 'Young Adult'
                     WHEN age > 30 AND age <= 55 THEN 'Adult'
                     WHEN age > 55 THEN 'Senior Citizen'
                     END AS 'Age_Group'
            FROM
            CTE C LEFT JOIN Clients CL on C.ClientID = CL.ClientID
    """
    df = pd.read_sql(sql, conn)

    df[['Latitude', 'Longtitude']] = df[['Latitude', 'Longtitude']].apply(pd.to_numeric)
    df['Age_Group'] = df['Age_Group'].astype('str')

    df['Latitude'].median()
    df['Longtitude'].median()

    map_options = GMapOptions(lat=37.686293, lng=-97.3614409, map_type="roadmap", zoom=10)

    p = gmap("My Google Maps API Key", map_options, title="Resolutions Clients Age wise",
             plot_width=1000, plot_height=600)

    lat = df['Latitude'].tolist()
    lon = df['Longtitude'].tolist()
    age = df['Age_Group'].tolist()

    source = ColumnDataSource(
        data=dict(latitude=lat,
                  longitude=lon,
                  age_Group=age
                  )
    )

    color_mapper = CategoricalColorMapper(factors=['Children', 'Young Adult', 'Adult', 'Senior Citizen'],
                                          palette=['Red', 'Blue', 'Green', 'Yellow'])

    p.add_tools(LassoSelectTool())
    p.add_tools(ZoomInTool())
    p.add_tools(ZoomOutTool())

    p.circle(x="longitude", y="latitude", size=4, fill_alpha=0.9, source=source,
             fill_color={'field': 'age_Group', 'transform': color_mapper},
             line_color={'field': 'age_Group', 'transform': color_mapper})

    x = np.linspace(0, 4 * np.pi, 100)
    y = np.sin(x)

    p.circle(x, y, legend="Children : Age <= 15", color="red")
    p.circle(x, 2 * y, legend="Young Adult : 30 >= Age > 15", color="Blue")
    p.circle(x, 3 * y, legend="Adult : 55 >= Age > 30 ", color="Green")
    p.circle(x, 4 * y, legend="Senior Citizen : Age > 55", color="Yellow")

    curdoc().add_root(row(button1, button2))
    curdoc().add_root(column(p))

def update1():
    # query db
    curdoc().clear()
    sql = """
            with CTE AS
                (SELECT
                CR.ClientID, CR.Latitude, CR.Longtitude
                FROM
                Company C LEFT JOIN ClientRegistration CR on C.CompanyID = CR.CompanyID
                WHERE
                C.CompanyID = 555 AND (CR.Latitude IS NOT NULL AND CR.Longtitude IS NOT NULL)
                )
            SELECT
            C.ClientID, C.Latitude, C.Longtitude, CL.Sex
            FROM
            CTE C LEFT JOIN Clients CL on C.ClientID = CL.ClientID
        """
    df = pd.read_sql(sql, conn)

    df[['Latitude', 'Longtitude']] = df[['Latitude', 'Longtitude']].apply(pd.to_numeric)
    df['Sex'] = df['Sex'].astype('str')

    map_options = GMapOptions(lat=37.686293, lng=-97.3614409, map_type="roadmap", zoom=10)
    p = gmap("My Google Maps API Key", map_options, title="Resolutions Clients Genderwise",
             plot_width=1000,
             plot_height=600)

    lat = df['Latitude'].tolist()
    lon = df['Longtitude'].tolist()
    sex = df['Sex'].tolist()

    source = ColumnDataSource(
        data=dict(latitude=lat,
                  longitude=lon,
                  gender=sex
                  )
    )


    color_mapper = CategoricalColorMapper(factors=['M', 'F', 'U'], palette=['Red', 'Blue', 'Green'])

    p.circle(x="longitude", y="latitude", size=4, fill_alpha=0.9, source=source,
             fill_color={'field': 'gender', 'transform': color_mapper},
             line_color={'field': 'gender', 'transform': color_mapper})

    x = np.linspace(0, 4 * np.pi, 100)
    y = np.sin(x)

    p.circle(x, y, legend="Male", color="red")
    p.circle(x, 2 * y, legend="Female", color="Blue")
    p.circle(x, 3 * y, legend="Unknown", color="Green")

    curdoc().add_root(row(button1, button2))
    curdoc().add_root(column(p))

# add a button widget and configure with the call back
button1 = Button(label="Gender")
button2 = Button(label="Age")

button1.on_click(update1)
button2.on_click(update)

# put the button and plot in a layout and add to the document
curdoc().add_root(row(button1, button2))
curdoc().add_root(column(p))
这是我的index.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Therasoft Visualization</title>
    <link rel="stylesheet" href={‌{url_for('static', filename='css/main.css')}}>
</head>
<body>
<h1> Therasoft Visualization</h1>
<div>
    {‌{bokeh_script|safe}}
</div>
</body>
</html>
编辑:在中解析


答案在另一期的代码中,您必须将值作为关键字args传递:

 p = gmap(google_api_key="My Google Maps API Key", map_options=map_options, ...)

我不确定这是为什么,这似乎是一个错误,通过args位置不起作用,但解决办法应该足够了

bigreddot,感谢您的回复,但即使将值作为关键字参数传递,问题仍然存在。
h3{
    color : olive;
}
 p = gmap(google_api_key="My Google Maps API Key", map_options=map_options, ...)