Skip to content

FigureWidget does work not from inside an if statement #1861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sursu opened this issue Nov 3, 2019 · 7 comments
Closed

FigureWidget does work not from inside an if statement #1861

sursu opened this issue Nov 3, 2019 · 7 comments

Comments

@sursu
Copy link
Contributor

sursu commented Nov 3, 2019

Encountered in Jupyter Lab.

Take the following 2 cases:

fig = go.FigureWidget()
fig

&

silent=False

if not silent:
    fig = go.FigureWidget()
    fig

The first one produces a figure, the second one does not.
Why don't they behave similarly?

If I use the @interact decorator in the second case.
e.g.

if not silent:
    fig = go.FigureWidget()
    fig.add_scatter()

    @interact(s=(3,5))
    def update(s=3):
        with fig.batch_update():
            fig.data[0].y = np.random.normal(size=s)
    fig

the slider appears, but the figure does not.

@nicolaskruchten
Copy link
Contributor

The way widgets work is that they render if they are the “output” of a cell, i.e. the last statement.

For @interact to work, the widget must be returned from the function.

@sursu
Copy link
Contributor Author

sursu commented Nov 3, 2019

@nicolaskruchten I undestand that it currently works like that. But, for the future, is there a reason for the widget not to work when returned from the if statement? An instance created with go.Figure() does.

@nicolaskruchten
Copy link
Contributor

if statements don't actually return anything, no. For a normal go.Figure() to work within an if statement you must call fig.show() on it.

These limitations aren't really Plotly limitations but rather Jupyter/Python limitations that we can't do much about :)

@nicolaskruchten
Copy link
Contributor

For example, this doesn't render anything, as expected:

image

@jasongrout
Copy link

jasongrout commented Nov 4, 2019

You may be able to use the display command:

if True:
    fig = go.Figure()
    display(fig)

(if you have an older version of ipython, you may need to import it: from IPython.display import display

@sursu
Copy link
Contributor Author

sursu commented Nov 4, 2019

TIL about display :)

Thanks a lot!

@jasongrout
Copy link

display is what IPython calls automatically on the last top-level expression to display it in your first example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants