jupyter-widgets / ipywidgets

Interactive Widgets for the Jupyter Notebook
https://ipywidgets.readthedocs.io
BSD 3-Clause "New" or "Revised" License
3.13k stars 948 forks source link

interact() for list (Dropdown or Combobox) does not respect default selected value #3870

Closed movy closed 9 months ago

movy commented 9 months ago

Description

interact() for a list (Dropdown or Combobox) does not respect the default selected value, i.e. regardless of value=.. the selected item is always list[0]

Reproduce

from ipywidgets import interact, widgets

list = [1,2,3]
def display(x):
    print(x)
interact(display, x=list, value=1)
interact(display, x=list, value=2)
from ipywidgets import interact, widgets

list = ["1","2","3"]
def display(x):
    print(x)
interact(display, x=widgets.Dropdown(options=list), value=list[0])
interact(display, x=widgets.Dropdown(options=list), value=list[1])
from ipywidgets import interact, widgets

list = ["1","2","3"]
def display(x):
    print(x)
interact(display, x=widgets.Combobox(options=list), value="1")
interact(display, x=widgets.Combobox(options=list), value="2")

Result always renders two identical widgets: image

Expected behavior

2nd widget is expected to have list[1] item selected

Context

✗ pip show ipywidgets
Name: ipywidgets
Version: 8.1.1
Summary: Jupyter interactive widgets
Home-page: http://jupyter.org
Author: Jupyter Development Team
Author-email: jupyter@googlegroups.com
License: BSD 3-Clause License
Location: /home/ubuntu/.local/lib/python3.11/site-packages
Requires: comm, ipython, jupyterlab-widgets, traitlets, widgetsnbextension
Required-by: jupyter

✗ python --version
Python 3.11.6
Troubleshoot Output
$PATH:
        /home/ubuntu/.local/bin
        /home/ubuntu/.vscode-server/bin/1a5daa3a0231a0fbba4f14db7ec463cf99d7768e/bin/remote-cli
        /home/ubuntu/.nvm/versions/node/v20.5.1/bin
        /home/ubuntu/.local/bin
        /usr/local/sbin
        /usr/local/bin
        /usr/sbin
        /usr/bin
        /sbin
        /bin
        /usr/games
        /usr/local/games
        /snap/bin

sys.path:
        /home/ubuntu/.local/bin
        /usr/lib/python311.zip
        /usr/lib/python3.11
        /usr/lib/python3.11/lib-dynload
        /home/ubuntu/.local/lib/python3.11/site-packages
        /usr/local/lib/python3.11/dist-packages
        /usr/lib/python3/dist-packages

sys.executable:
        /usr/bin/python3

sys.version:
        3.11.6 (main, Oct 23 2023, 22:48:54) [GCC 11.4.0]

platform.platform():
        Linux-6.5.0-1008-oem-x86_64-with-glibc2.35

which -a jupyter:
        /home/ubuntu/.local/bin/jupyter
        /home/ubuntu/.local/bin/jupyter

pip list:
        Package                   Version
        ------------------------- -------------
        aiofiles                  0.7.0
        aiohttp                   3.9.0
        aiohttp-cors              0.7.0
        aioredis                  1.3.1
        aiorwlock                 1.3.0
        aiosignal                 1.3.1
        alembic                   1.12.1
        anyio                     3.7.1
        appdirs                   1.4.4
        argon2-cffi               23.1.0
        argon2-cffi-bindings      21.2.0
        arrow                     1.2.1
        asttokens                 2.4.1
        async-lru                 2.0.4
        async-timeout             4.0.3
        attrs                     23.1.0
        Babel                     2.13.1
        beautifulsoup4            4.12.2
        bitarray                  2.8.3
        bleach                    6.1.0
        blessed                   1.20.0
        blinker                   1.4
        cachetools                5.3.2
        certifi                   2023.11.17
        cffi                      1.16.0
        charset-normalizer        3.3.2
        click                     8.1.7
        cloudpickle               3.0.0
        cmaes                     0.10.0
        colorful                  0.5.5
        colorlog                  6.7.0
        comm                      0.2.0
        ConfigSpace               0.7.1
        contourpy                 1.2.0
        cryptography              3.4.8
        cycler                    0.12.1
        Cython                    3.0.5
        cytoolz                   0.12.1
        dateparser                1.0.0
        dbus-python               1.2.18
        debugpy                   1.8.0
        decorator                 5.1.1
        defusedxml                0.7.1
        Deprecated                1.2.14
        distlib                   0.3.7
        distro                    1.7.0
        distro-info               1.1+ubuntu0.1
        ecdsa                     0.18.0
        eth-abi                   4.2.1
        eth-account               0.8.0
        eth-hash                  0.5.2
        eth-keyfile               0.6.1
        eth-keys                  0.4.0
        eth-rlp                   0.3.0
        eth-typing                3.5.2
        eth-utils                 2.3.1
        executing                 2.0.1
        fastapi                   0.104.1
        fastjsonschema            2.19.0
        filelock                  3.13.1
        fonttools                 4.45.0
        fqdn                      1.5.1
        frozendict                2.3.8
        frozenlist                1.4.0
        fsspec                    2023.10.0
        future                    0.18.3
        google-api-core           2.14.0
        google-auth               2.23.4
        googleapis-common-protos  1.61.0
        gpustat                   1.1.1
        GPy                       1.10.0
        greenlet                  3.0.1
        grpcio                    1.59.3
        h11                       0.14.0
        hexbytes                  0.3.1
        hiredis                   2.2.3
        hpbandster                0.7.4
        html5lib                  1.1
        httplib2                  0.20.2
        httptools                 0.6.1
        hyperopt                  0.2.7
        idna                      3.4
        importlib-metadata        4.6.4
        iniconfig                 2.0.0
        ipykernel                 6.26.0
        ipython                   8.17.2
        ipywidgets                8.1.1
        isoduration               20.11.0
        jedi                      0.19.1
        jeepney                   0.7.1
        jesse                     0.44.0
        jesse-dydx-v3-python      1.0.4
        Jinja2                    3.1.2
        joblib                    1.3.2
        json5                     0.9.14
        jsonpointer               2.4
        jsonschema                4.20.0
        jsonschema-specifications 2023.11.1
        jupyter                   1.0.0
        jupyter_client            8.6.0
        jupyter-console           6.6.3
        jupyter_core              5.5.0
        jupyter-events            0.9.0
        jupyter-lsp               2.2.0
        jupyter_server            2.10.1
        jupyter_server_terminals  0.4.4
        jupyterlab                4.0.9
        jupyterlab-pygments       0.2.2
        jupyterlab_server         2.25.2
        jupyterlab-widgets        3.0.9
        keyring                   23.5.0
        kiwisolver                1.4.5
        launchpadlib              1.10.16
        lazr.restfulclient        0.14.4
        lazr.uri                  1.0.6
        lru-dict                  1.3.0
        lxml                      4.9.3
        Mako                      1.3.0
        MarkupSafe                2.1.3
        matplotlib                3.8.2
        matplotlib-inline         0.1.6
        mistune                   3.0.2
        more-itertools            8.10.0
        mplfinance                0.12.8b6
        mpmath                    1.0.0
        msgpack                   1.0.7
        multidict                 6.0.4
        multitasking              0.0.11
        nbclient                  0.9.0
        nbconvert                 7.11.0
        nbformat                  5.9.2
        nest-asyncio              1.5.8
        netifaces                 0.11.0
        networkx                  3.2.1
        newtulipy                 0.4.6
        notebook                  7.0.6
        notebook_shim             0.2.3
        notebookjs                0.1.4
        numpy                     1.24.0
        numpy-groupies            0.9.14
        nvidia-ml-py              12.535.133
        oauthlib                  3.2.0
        opencensus                0.11.3
        opencensus-context        0.1.3
        optuna                    3.4.0
        orjson                    3.9.10
        overrides                 7.4.0
        packaging                 23.2
        pandas                    2.1.3
        pandocfilters             1.5.0
        paramz                    0.9.5
        parsimonious              0.9.0
        parso                     0.8.3
        patsy                     0.5.3
        peewee                    3.14.8
        pexpect                   4.8.0
        Pillow                    10.1.0
        pip                       23.3.1
        platformdirs              3.11.0
        pluggy                    1.3.0
        prometheus-client         0.19.0
        prompt-toolkit            3.0.41
        protobuf                  4.25.1
        psutil                    5.9.6
        psycopg2-binary           2.9.4
        ptyprocess                0.7.0
        pure-eval                 0.2.2
        py                        1.11.0
        py-spy                    0.3.14
        py4j                      0.10.9.7
        pyarrow                   14.0.1
        pyasn1                    0.5.1
        pyasn1-modules            0.3.0
        pycparser                 2.21
        pycryptodome              3.19.0
        pydantic                  1.10.13
        pydash                    6.0.0
        Pygments                  2.17.1
        PyGObject                 3.42.1
        pyjson5                   1.6.4
        PyJWT                     2.3.0
        pyparsing                 2.4.7
        Pyro4                     4.82
        pytest                    6.2.5
        python-apt                2.4.0+ubuntu2
        python-dateutil           2.8.2
        python-dotenv             1.0.0
        python-json-logger        2.0.7
        python-magic              0.4.24
        pytz                      2023.3.post1
        pyunormalize              15.1.0
        PyWavelets                1.2.0
        PyYAML                    6.0.1
        pyzmq                     25.1.1
        qtconsole                 5.5.1
        QtPy                      2.4.1
        QuantStats                0.0.47
        ray                       3.0.0.dev0
        redis                     4.1.4
        referencing               0.31.0
        regex                     2023.10.3
        requests                  2.31.0
        rfc3339-validator         0.1.4
        rfc3986-validator         0.1.1
        rlp                       3.0.0
        rpds-py                   0.13.1
        rsa                       4.9
        scikit-learn              1.3.2
        scipy                     1.11.4
        seaborn                   0.13.0
        SecretStorage             3.3.1
        Send2Trash                1.8.2
        serpent                   1.41
        setuptools                69.0.0
        simplejson                3.16.0
        six                       1.16.0
        smart-open                6.4.0
        sniffio                   1.3.0
        sos                       4.5.6
        soupsieve                 2.5
        SQLAlchemy                2.0.23
        ssh-import-id             5.11
        stack-data                0.6.3
        starlette                 0.27.0
        statsmodels               0.14.0
        sympy                     1.6
        TA-Lib                    0.4.28
        tabulate                  0.8.9
        tensorboardX              2.6.2.2
        terminado                 0.18.0
        threadpoolctl             3.2.0
        timeloop                  1.0.2
        tinycss2                  1.2.1
        toml                      0.10.2
        toolz                     0.12.0
        tornado                   6.3.3
        tqdm                      4.66.1
        traitlets                 5.13.0
        types-python-dateutil     2.8.19.14
        typing_extensions         4.8.0
        tzdata                    2023.3
        tzlocal                   5.2
        ubuntu-advantage-tools    8001
        ufw                       0.36.1
        unattended-upgrades       0.1
        uri-template              1.3.0
        urllib3                   2.1.0
        uvicorn                   0.24.0.post1
        uvloop                    0.19.0
        virtualenv                20.21.0
        wadllib                   1.3.6
        watchfiles                0.21.0
        wcwidth                   0.2.11
        web3                      6.10.0
        webcolors                 1.13
        webencodings              0.5.1
        websocket-client          1.2.3
        websockets                12.0
        wheel                     0.41.3
        widgetsnbextension        4.0.9
        wrapt                     1.16.0
        yarl                      1.9.3
        yfinance                  0.2.28
        zipp                      1.0.0
Command Line Output
Paste the output from your command line running `jupyter lab` (or `jupyter notebook` if you use notebook) here, use `--debug` if possible.
Browser Output
Paste the output from your browser Javascript console here.

If using JupyterLab

Installed Labextensions
 jupyter labextension list
JupyterLab v4.0.9
/home/ubuntu/.local/share/jupyter/labextensions
        jupyterlab_pygments v0.2.2 enabled  X (python, jupyterlab_pygments)
        @jupyter-widgets/jupyterlab-manager v5.0.9 enabled OK (python, jupyterlab_widgets)
        @jupyter-notebook/lab-extension v7.0.6 enabled OK

   The following extensions are outdated:
        jupyterlab_pygments

   Consider checking if an update is available for these packages.
ibdafna commented 9 months ago

This is working as expected. The interact function doesn't take a parameter for value - try changing the value parameter in the first interact line and you'll see it makes no difference. I'd recommend taking a look at the tutorial to see how to set a default value for your dropdown.