简体   繁体   English

Dash python:如何用csv内容填充dcc.dropdown?

[英]Dash python : How to fill dcc.dropdown with csv content?

I would like to create an web app in python and Dash.我想用 python 和 Dash 创建一个网络应用程序。 The first that I try is to create a dropdown.我尝试的第一个方法是创建一个下拉列表。

I've these data :我有这些数据:

Date,FS,Total,Used,Mount
2020-01-25-12-00,/dev/hd1/,350,300,/dev/mount1
2020-01-25-18-00,/dev/hd2/,370,320,/dev/mount2
2020-01-26-06-00,/dev/hd3/,395,350,/dev/mount3
2020-01-26-12-00,/dev/hd1/,350,300,/dev/mount1
2020-01-26-18-00,/dev/hd2/,370,320,/dev/mount2
2020-01-27-06-00,/dev/hd3/,395,350,/dev/mount3
2020-01-27-12-00,/dev/hd1/,350,300,/dev/mount1
2020-01-27-18-00,/dev/hd2/,370,320,/dev/mount2
2020-01-28-06-00,/dev/hd3/,395,350,/dev/mount3
2020-01-28-12-00,/dev/hd1/,350,300,/dev/mount1
2020-01-28-18-00,/dev/hd2/,370,320,/dev/mount2
2020-01-29-06-00,/dev/hd3/,395,350,/dev/mount3

I would like to create a dropdown with all the FS of my CSV.我想用我的 CSV 的所有 FS 创建一个下拉列表。 I try that :我试试:

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

app = dash.Dash


df = pd.read_csv('/xxx/xxx/xxx/xxx/xxx/xxx/data.txt')

test = df['FS'].unique()

dcc.Dropdown(
    options=[test],
    searchable=False
)  

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

But the output is但输出是

Traceback (most recent call last):
  File "./import dash.py", line 25, in <module>
    app.run_server(debug=True)
TypeError: run_server() missing 1 required positional argument: 'self'

Can you tell me why ?你能告诉我为什么吗 ?

There are some issues in your code:您的代码中存在一些问题:

  1. You have to assign a name to your dash application, eg app = dash.Dash('app_name')您必须为破折号应用程序指定一个名称,例如app = dash.Dash('app_name')

  2. Your csv has mixed separators ( , in header and ; in tuples)您的 csv 有混合分隔符( ,在标题中和;在元组中)

  3. The dropdown-options syntax is {'label: 'somelabel', 'value':'somevalue'} , eg (ref. Dropdown Examples and Reference )下拉选项语法是{'label: 'somelabel', 'value':'somevalue'} ,例如(参考。下拉示例和参考

options=[
        {'label': 'New York City', 'value': 'NYC'},
        {'label': 'Montreal', 'value': 'MTL'},
        {'label': 'San Francisco', 'value': 'SF'}
    ],
  1. You have to assign your components to app.layout您必须将组件分配给app.layout

To sum up:总结:

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


app = dash.Dash('app_name')

df = pd.read_csv('/xxx/xxx/xxx/xxx/xxx/xxx/data.txt')

test = df['FS'].unique()
options = [{'label': t, 'value': t} for t in test]

app.layout = dcc.Dropdown(
    options=options,
    searchable=False
    )

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 清除所有 Dash dcc.Dropdown 问题 - Dash dcc.Dropdown issue with clear all 动态设置 Plotly Dash dcc.dropdown 值 - Setting a Plotly Dash dcc.dropdown value dynamically 在dash应用程序中,可以在dcc.Dropdown中的值中实现href吗? - In the dash app, can href be implemented in to the value within in dcc.Dropdown? 当从 dcc.dropdown 菜单中选择了几个选项时,我需要一个 div 被按下(破折号组件,python) - I need a div to be pushed down when several options from a dcc.dropdown menu have been selected (dash component, python) 是否可以从 Plotly Dash、dcc.store 中的值动态加载 dcc.Dropdown 选项? - Is it possible to dynamically load dcc.Dropdown option from values in Plotly Dash, dcc.store? 尝试使用 dcc.Dropdown (multi = True) 在 Dash-Plotly (Python) 中显示/隐藏 html.Div 元素 - Trying to use dcc.Dropdown (multi = True) to show/hide html.Div elements in Dash-Plotly (Python) 如何更新 Dash dcc 中的链式下拉值? - How to update chained dropdown value in Dash dcc? How to dynamically set a default value in a dcc.Dropdown when options come from a callback? - How to dynamically set a default value in a dcc.Dropdown when options come from a callback? 基于 RadioItesm dcc 更新 plotly dash Dropdown dcc - Updating plotly dash Dropdown dcc based on a RadioItesm dcc 如何在 Python Dash 中的两个 dcc 组件之间留出空间? - How to give space between two dcc components in Python Dash?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM