简体   繁体   中英

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

I would like to create an web app in python and 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. 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')

  2. Your csv has mixed separators ( , in header and ; in tuples)

  3. The dropdown-options syntax is {'label: 'somelabel', 'value':'somevalue'} , eg (ref. Dropdown Examples and Reference )

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM