首页 > 解决方案 > Dynamically Fill Out Dropdown in Dash

问题描述

I have an issue that after I run my sql statement in a data frame. I'm sure I'm missing something easy but its holding me up big time and I'm having issues getting my dashboard project even off the ground.

In the below code All I can get in the dropdown is UNUM when I run it and I can't figure out what I'm doing wrong to get my values out of it. I can print eqids with no issue but I can't iterate over it correctly. Thanks for the assistance

import dash
import dash_bootstrap_components as dbc
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output
import dash_table

import pandas as pd
import sqlite3

import plotly.graph_objs as go

external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']

app = dash.Dash(__name__, external_stylesheets=external_stylesheets)

conn = sqlite3.connect(
    r"mysqlfile")
c = conn.cursor()

eqids = pd.read_sql("SELECT distinct UNUM FROM AllTestData", conn)
#names = list(eqids.keys())
print(eqids)
'''df = pd.read_sql("select * from AllTestData", conn)
df = df[['UNUM', 'DT_TAKEN', 'Iron', 'Chromium', 'Lead', 'Copper', 'Tin', 'Aluminum', 'Nickel', 'Silver', 'Silicon', 'boron', 'Sodium', 'Magnesium', 'Calcium', 'Barium', 'Phosphorous', 'Zinc',
         'Molybdenum', 'Tin1', 'Vandium', 'W', 'Potassium', 'Antimony', 'Lithium', 'Maganese', 'Cadmium', 'VISC40', 'TAN', 'KFISH', 'WATER', 'PC0', 'PC1', 'pc2', 'pc3', 'pc4', 'pc5', 'PCISO0', 'PCISO1', 'PCISO2']]
df.head(1)
'''

app.layout = html.Div([
    html.Label('Select Equipment ID'),
    dcc.Dropdown(
        id='equipment-list',
        options=[{'label': i, 'value': i} for i in eqids],
    ),
    dcc.Graph(id='test-graph')
    # html.Div(id='test-graph')
])


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

标签: pythonpython-3.xplotly-dash

解决方案


哦,我的选项代码中需要这个 eqids.UNUM。


推荐阅读