dash icon indicating copy to clipboard operation
dash copied to clipboard

[BUG] - selectedData not populated when graph with selection loads

Open LiamConnors opened this issue 3 years ago • 2 comments
trafficstars

Describe your context Please provide us your environment, so we can easily reproduce the issue.

  • replace the result of pip list | grep dash below
Package              Version                   Location
-------------------- ------------------------- --------------------------------------------------------------------
anyio                3.5.0
appdirs              1.4.4
appnope              0.1.3
argon2-cffi          21.3.0
argon2-cffi-bindings 21.2.0
asttokens            2.0.5
attrs                21.4.0
Babel                2.9.1
backcall             0.2.0
backports.tempfile   1.0
backports.weakref    1.0.post1
beautifulsoup4       4.10.0
black                22.3.0
bleach               4.1.0
Brotli               1.0.9
certifi              2021.10.8
cffi                 1.15.0
cfgv                 3.3.1
charset-normalizer   2.0.12
click                8.1.2
click-plugins        1.1.1
cligj                0.7.2
colorcet             3.0.0
coverage             4.3.1
dash                 2.6.0
dash-core-components 2.0.0
dash-html-components 2.0.0
dash-table           5.0.0
debugpy              1.6.0
decorator            5.1.1
defusedxml           0.7.1
distlib              0.3.4
entrypoints          0.4
executing            0.8.3
fastjsonschema       2.15.3
filelock             3.6.0
Fiona                1.8.21
Flask                2.1.3
Flask-Compress       1.12
geopandas            0.10.2
identify             2.4.12
idna                 3.3
importlib-metadata   4.11.3
inflect              5.4.0
ipykernel            6.12.1
ipython              8.2.0
ipython-genutils     0.2.0
ipywidgets           7.7.0
itsdangerous         2.1.2
jedi                 0.18.1
Jinja2               3.1.1
json5                0.9.6
jsonschema           4.4.0
jupyter              1.0.0
jupyter-client       7.2.1
jupyter-console      6.4.3
jupyter-core         4.9.2
jupyter-server       1.16.0
jupyterlab           3.3.2
jupyterlab-pygments  0.1.2
jupyterlab-server    2.12.0
jupyterlab-widgets   1.1.0
jupytext             1.13.7
markdown-it-py       1.1.0
MarkupSafe           2.1.1
matplotlib-inline    0.1.3
mdit-py-plugins      0.3.0
mistune              0.8.4
mock                 2.0.0
more-itertools       8.12.0
munch                2.5.0
mypy-extensions      0.4.3
nbclassic            0.3.7
nbclient             0.5.13
nbconvert            6.4.5
nbformat             5.3.0
nest-asyncio         1.5.5
nodeenv              1.6.0
notebook             6.4.10
notebook-shim        0.1.0
numpy                1.22.3
packaging            21.3
pandas               1.4.2
pandocfilters        1.5.0
param                1.12.1
parso                0.8.3
pathspec             0.9.0
pbr                  5.8.1
pexpect              4.8.0
pickleshare          0.7.5
Pillow               9.1.0
pip                  21.2.4
platformdirs         2.5.1
plotly               (built from latest Plotly.js)
pluggy               0.6.0
pre-commit           2.18.1
prometheus-client    0.13.1
prompt-toolkit       3.0.28
psutil               5.9.0
ptyprocess           0.7.0
pure-eval            0.2.2
py                   1.11.0
pycparser            2.21
pyct                 0.4.8
Pygments             2.11.2
pyparsing            3.0.7
pyproj               3.3.0
pyrsistent           0.18.1
pyshp                2.2.0
pytest               3.5.1
python-dateutil      2.8.2
pytz                 2022.1
PyYAML               6.0
pyzmq                22.3.0
qtconsole            5.3.0
QtPy                 2.0.1
regex                2022.3.15
requests             2.27.1
scipy                1.8.0
Send2Trash           1.8.0
setuptools           58.0.4
Shapely              1.8.1.post1
six                  1.16.0
sniffio              1.2.0
soupsieve            2.3.1
stack-data           0.2.0
tenacity             8.0.1
terminado            0.13.3
testpath             0.6.0
toml                 0.10.2
tomli                2.0.1
tornado              6.1
traitlets            5.1.1
typed-ast            1.5.2
typing_extensions    4.1.1
urllib3              1.26.9
virtualenv           20.14.0
wcwidth              0.2.5
webencodings         0.5.1
websocket-client     1.3.2
Werkzeug             2.1.2
wheel                0.37.1
widgetsnbextension   3.6.0
xarray               2022.3.0
zipp                 3.7.0

Describe the bug

When creating a graph that has a selection pre-selected, this isn't shown in selectedData when the graph loads. The user needs to move or alter the selection for something to populate in selectedData

Expected behavior

selectedData would show

Screenshots Graph on load:

image

After moving the selection:

image

Example code:

import json

from dash import Dash, dcc, html
from dash.dependencies import Input, Output
import plotly.express as px

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

app = Dash(__name__, external_stylesheets=external_stylesheets)

styles = {
    'pre': {
        'border': 'thin lightgrey solid',
        'overflowX': 'scroll'
    }
}

df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color='petal_length')

fig.add_selection(type="rect",
    x0=3.0, y0=6.5, x1=3.5, y1=5.5,
    line=dict(
        color="Crimson",
        width=2,
        dash="dash",
    ))

fig.update_layout(dragmode='select',
                  newselection=dict(line=dict(color='blue')))

app.layout = html.Div([
    dcc.Graph(
        id='basic-interactions-3',
        figure=fig
    ),

        html.Div([
            dcc.Markdown("""
                **Selection Data**

            """),
            html.Pre(id='selected-data-3', style=styles['pre']),
        ]),

])


@app.callback(
    Output('selected-data-3', 'children'),
    Input('basic-interactions-3', 'selectedData'))
def display_selected_data(selectedData):
    return json.dumps(selectedData, indent=2)


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




@archmoj

LiamConnors avatar Jul 15 '22 20:07 LiamConnors

@alexcjohnson could it be related to data revision somehow?

archmoj avatar Jul 15 '22 20:07 archmoj

I guess what's happening is there's no plotly_selected event generated when you first draw a plot having specified a selection, so dcc.Graph can't populate selectedData?

I think it's pretty important for apps that would use this to be able to get the correct selectedData immediately after creating the plot - basically using plotly.js to apply the selection they gave and tell them the result. I don't think it would be a breaking change (at least relative to pre-2.13 plotly.js) to say "if you provide layout.selections to the plot, it should emit a plotly_selected right away", would you agree?

alexcjohnson avatar Jul 15 '22 21:07 alexcjohnson