如何使用Dash,熊猫,mysql和python填充下拉列表

时间:2019-09-10 16:27:40

标签: python mysql pandas flask

我正在尝试使用combo box dash_core_components html python来填充mysql

这是我的联系dbCon

import MySQLdb
from sqlalchemy import types, create_engine

# MySQL Connection
MYSQL_USER = 'root'
MYSQL_PASSWORD = 'xxxxxxxxxx'
MYSQL_HOST_IP = 'xxxxxxxxxx'
MYSQL_PORT = 3306
MYSQL_DATABASE = 'gov_tracker_db'
govtracker_table = 'gov_tracker'  # table you want to save

engine = create_engine('mysql+mysqlconnector://' + MYSQL_USER + ':' + MYSQL_PASSWORD + '@' + MYSQL_HOST_IP + ':' + str(
    MYSQL_PORT) + '/' + MYSQL_DATABASE, echo=False)

con = MySQLdb.connect(user=MYSQL_USER, passwd=MYSQL_PASSWORD, host=MYSQL_HOST_IP, db=MYSQL_DATABASE)

这是我的pandas已读过myPandas

from dbCon import engine
import pandas as pd

site_code_col = pd.read_sql('SELECT site_code FROM gov_tracker', engine).dropna().to_dict(orient='records')
col_val = pd.read_sql('SELECT pending, COUNT(*) AS DuplicatesCount FROM gov_tracker', engine).to_dict(orient='records')
pending_col = pd.read_sql('SELECT pending FROM gov_tracker', engine).drop_duplicates().dropna().to_dict(orient='records')
col_name = pd.read_sql('EXPLAIN gov_tracker', engine).to_dict(orient='records')

这是我的应用app

import dash
import dash_core_components as dcc
import dash_html_components as html
from myPandas import site_code_col, col_val, col_name

app = dash.Dash(__name__)

app.layout = html.Div([

    html.H1(children='ComboBox'),

    html.Div(children='''Choose'''),

    html.Label('Site Code'),

    dcc.Dropdown(
        id="scInput",
        options=[{'label': i['site_code'], 'value': i['site_code']} for i in site_code_col],
        multi=True,
        value="MTL"
    ),

    dcc.Dropdown(
        id="colNames",
        options=[{'label': i['gov_tracker'], 'value': i['gov_tracker']} for i in col_name],
        multi=True,
        value="MTL"
    ),


    html.Button('Submit', id='button'),
    html.Div(id='scOutput'),

])

if __name__ == '__main__':
    app.run_server()

这是我发现的错误

Traceback (most recent call last):
  File "C:/Users/DELL/PycharmProjects/Gov-Trac-Tool/app.py", line 25, in <module>
    options=[{'label': i['gov_tracker'], 'value': i['gov_tracker']} for i in col_name],
  File "C:/Users/DELL/PycharmProjects/Gov-Trac-Tool/app.py", line 25, in <listcomp>
    options=[{'label': i['gov_tracker'], 'value': i['gov_tracker']} for i in col_name],
KeyError: 'gov_tracker'

注意:“ gov_tracker”是表名,其中包含我需要在combobox

中填充的列

这是第一部分,第二部分是如何与Flask

进行交互

0 个答案:

没有答案