Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

dcc.Dropdown value returns unicode in Python 2 and string in Python 3 #783

@shammamah-zz

Description

@shammamah-zz

Seen in https://github.com/plotly/dash-docs/pull/813

Minimal example:

# -*- coding: utf-8 -*-
import dash
from dash.dependencies import Input, Output
import dash_core_components as dcc
import dash_html_components as html
import pandas as pd

import plotly.express as px

app = dash.Dash(__name__)

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/gapminderDataFiveYear.csv')

available_countries = df['country'].unique()

app.layout = html.Div([
    dcc.Graph(
        id='graph-px'
    ),
    'Indicator',
    dcc.Dropdown(
        id='graph-indicator-px',
        options=[
            {'label': 'Population', 'value': 'pop'},
            {'label': 'Life Expectancy', 'value': 'lifeExp'},
            {'label': 'GDP per Capita', 'value': 'gdpPercap'}
        ],
        value='pop'
    ),
    html.Div(id='indicator-type')
])


@app.callback(
    Output('graph-px', 'figure'),
    [Input('graph-indicator-px', 'value')]
)
def update_store_data(indicator):
    dff = df[df['country'] == 'Canada']
    return px.scatter(dff, x='year', y=indicator)


@app.callback(
    Output('indicator-type', 'children'),
    [Input('graph-indicator-px', 'value')]
)
def update_indicator_type(indicator):
    return str(type(indicator))


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

python-2.7:
Screen Shot 2020-03-24 at 11 46 11 AM

python-3.7:
Screen Shot 2020-03-24 at 11 46 32 AM

Consequently, the column name (set by the dropdown) gets interpreted as an iterable itself, and if the length of the column name is not the same as the number of elements in the column, there is an error in plotly.express below:
https://github.com/plotly/plotly.py/blob/60e366645138836cbaf3122e3033d8db35d9caf9/packages/python/plotly/plotly/express/_core.py#L1016

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions