使用python中的panda library和Dash Plotly创建带有标签和值的下拉列表

使用python中的panda library和Dash Plotly创建带有标签和值的下拉列表,python,pandas,plotly,plotly-dash,Python,Pandas,Plotly,Plotly Dash,我使用Python中的panda库读取csv文件并填充下拉列表。我的应用程序使用Dash Plotly创建HTML web界面。我只填写下拉列表的值,下拉列表的标签与值相同。如何将标签更改为csv文件中的文本 可用的\u rpi.csv ip,name 192.168.1.6,"Virtual I²C (192.168.1.6)" 192.168.1.102,"GPS UART (192.168.1.102)" 192.168.1.106,"Ultrasonic I²C (192.168.1.1

我使用Python中的panda库读取csv文件并填充下拉列表。我的应用程序使用Dash Plotly创建HTML web界面。我只填写下拉列表的值,下拉列表的标签与值相同。如何将标签更改为csv文件中的文本

可用的\u rpi.csv

ip,name
192.168.1.6,"Virtual I²C (192.168.1.6)"
192.168.1.102,"GPS UART (192.168.1.102)"
192.168.1.106,"Ultrasonic I²C (192.168.1.103)"
python脚本:

import dash,requests,pandas as pd

df = pd.read_csv('available_rpi.csv', usecols = ['ip','name'])
available_rpi = df['ip'].unique()

app.layout = html.Div( [
    html.H1(children='RESENSE'),
    html.Div(children='''RESENSE: Transparent Record and Replay in the Internet of Things (IoT).'''),
    # html.Div(['Name : ', dcc.Input(id='input',value='ACC',type='text') ]),
    # dcc.Markdown(''' '''),
    html.Label('Raspberry Pi'),
    dcc.Dropdown(
        id = "input",
        options=[{'label': i, 'value': i} for i in available_rpi],
        value=''
    ),
    html.Div(id='output'),
    #   Graph for arriving data (static)
    dcc.Graph(id='data', animate=True),
    dcc.Interval(id='graph-update',interval=2*1000)
    ])

您应该使用
orient='records'
将.csv文件存储为字典列表,然后使用列表理解设置下拉组件的选项:

import dash
import pandas as pd
import dash_core_components as dcc
import dash_html_components as html

available_rpi = pd.read_csv('available_rpi.csv').to_dict(orient='records')

app = dash.Dash(__name__)

app.layout = html.Div([

    html.H1(children='RESENSE'),

    html.Div(children='''RESENSE: Transparent Record and Replay in the Internet of Things (IoT).'''),

    html.Label('Raspberry Pi'),

    dcc.Dropdown(
        id = "input",
        options=[{'label': i['name'], 'value': i['ip']} for i in available_rpi],
        value=''
    ),

    html.Div(id='output'),

    #Graph for arriving data (static)
    dcc.Graph(id='data', animate=True),

    dcc.Interval(id='graph-update',interval=2*1000)

])

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

您应该使用
orient='records'
将.csv文件存储为字典列表,然后使用列表理解设置下拉组件的选项:

import dash
import pandas as pd
import dash_core_components as dcc
import dash_html_components as html

available_rpi = pd.read_csv('available_rpi.csv').to_dict(orient='records')

app = dash.Dash(__name__)

app.layout = html.Div([

    html.H1(children='RESENSE'),

    html.Div(children='''RESENSE: Transparent Record and Replay in the Internet of Things (IoT).'''),

    html.Label('Raspberry Pi'),

    dcc.Dropdown(
        id = "input",
        options=[{'label': i['name'], 'value': i['ip']} for i in available_rpi],
        value=''
    ),

    html.Div(id='output'),

    #Graph for arriving data (static)
    dcc.Graph(id='data', animate=True),

    dcc.Interval(id='graph-update',interval=2*1000)

])

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

我得用字典

available_rpi = pd.read_csv('available_rpi.csv', header=None, dtype={0: str}).set_index(0).squeeze().to_dict()
#print("Raspberry Pi's available:")
#for key, car in available_rpi.items():
#    print('{} : {}'.format(key, car))

app.layout = html.Div( [
    html.H1(children='RESENSE'),
    html.Div(children='''RESENSE: Transparent Record and Replay in the Internet of Things (IoT).'''),
    # html.Div(['Name : ', dcc.Input(id='input',value='ACC',type='text') ]),
    # dcc.Markdown(''' '''),
    html.Label('Raspberry Pi'),
    dcc.Dropdown(
        id = "input",
        options=[{'label': v, 'value': k} for k, v in available_rpi.items()],
        value=''
    ),
    html.Div(id='output'),
    #   Graph for arriving data (static)
    dcc.Graph(id='data', animate=True),
    dcc.Interval(id='graph-update',interval=2*1000)
    ])

我得用字典

available_rpi = pd.read_csv('available_rpi.csv', header=None, dtype={0: str}).set_index(0).squeeze().to_dict()
#print("Raspberry Pi's available:")
#for key, car in available_rpi.items():
#    print('{} : {}'.format(key, car))

app.layout = html.Div( [
    html.H1(children='RESENSE'),
    html.Div(children='''RESENSE: Transparent Record and Replay in the Internet of Things (IoT).'''),
    # html.Div(['Name : ', dcc.Input(id='input',value='ACC',type='text') ]),
    # dcc.Markdown(''' '''),
    html.Label('Raspberry Pi'),
    dcc.Dropdown(
        id = "input",
        options=[{'label': v, 'value': k} for k, v in available_rpi.items()],
        value=''
    ),
    html.Div(id='output'),
    #   Graph for arriving data (static)
    dcc.Graph(id='data', animate=True),
    dcc.Interval(id='graph-update',interval=2*1000)
    ])

使用pandas以稍微不同的方式读取CSV数据并将其存储在字典中如何

import dash
import pandas as pd
import dash_core_components as dcc
import dash_html_components as html

df = pd.read_csv('available_rpi.csv', usecols = ['ip','name'])
available_rpi = df.to_dict('records')

app = dash.Dash(__name__)

app.layout = html.Div( [
    html.H1(children='RESENSE'),
    html.Div(children='''RESENSE: Transparent Record and Replay in the Internet of Things (IoT).'''),
    # html.Div(['Name : ', dcc.Input(id='input',value='ACC',type='text') ]),
    # dcc.Markdown(''' '''),
    html.Label('Raspberry Pi'),
    dcc.Dropdown(
        id = "input",
        options=[{'label': i['name'], 'value': i['ip']} for i in available_rpi],
        value=''
    ),
    html.Div(id='output'),
    #   Graph for arriving data (static)
    dcc.Graph(id='data', animate=True),
    dcc.Interval(id='graph-update',interval=2*1000)
    ])

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

使用pandas以稍微不同的方式读取CSV数据并将其存储在字典中如何

import dash
import pandas as pd
import dash_core_components as dcc
import dash_html_components as html

df = pd.read_csv('available_rpi.csv', usecols = ['ip','name'])
available_rpi = df.to_dict('records')

app = dash.Dash(__name__)

app.layout = html.Div( [
    html.H1(children='RESENSE'),
    html.Div(children='''RESENSE: Transparent Record and Replay in the Internet of Things (IoT).'''),
    # html.Div(['Name : ', dcc.Input(id='input',value='ACC',type='text') ]),
    # dcc.Markdown(''' '''),
    html.Label('Raspberry Pi'),
    dcc.Dropdown(
        id = "input",
        options=[{'label': i['name'], 'value': i['ip']} for i in available_rpi],
        value=''
    ),
    html.Div(id='output'),
    #   Graph for arriving data (static)
    dcc.Graph(id='data', animate=True),
    dcc.Interval(id='graph-update',interval=2*1000)
    ])

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

不起作用。您正在将
i
用于
label
value
以满足您的要求。一开始误读了你的问题,我的答案被编辑了。不起作用。您正在将
i
用于
label
value
以满足您的要求。一开始误读了你的问题,我的答案被编辑了。顺便说一句,这仍然不是一个完整的破折号应用程序。你缺少必要的导入库,以及基本结构是的。它不完整,我只是复制了我有问题的部分。我没有复制导入这仍然不是一个完整的破折号应用程序。顺便说一句,您缺少必要的导入库以及基本结构是的。它不完整,我只是复制了我有问题的部分。我没有复制导入内容