Skip to content
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

Capturing matplotlib figures in Jupyter notebooks #2897

Open
z0u opened this issue Feb 25, 2025 · 1 comment
Open

Capturing matplotlib figures in Jupyter notebooks #2897

z0u opened this issue Feb 25, 2025 · 1 comment

Comments

@z0u
Copy link

z0u commented Feb 25, 2025

I'm using Modal in a Jupyter notebook, as shown in the guide. It works, but I'm having trouble with the cell output.

@app.function()
def train(epochs):
    from time import sleep
    from IPython.display import clear_output
    import matplotlib.pyplot as plt

    # Iteratively plot a simple function to demonstrate
    xs = []
    ys = []
    for i in range(epochs):
        clear_output(wait=True)
        xs.append(i)
        ys.append(i**2)
        plt.plot(xs, ys)
        plt.show()
        sleep(1)
with modal.enable_output(), app.run():
    #train.local(5)
    train.remote(5)

The plot displays when running locally, but not remotely. I'd like to be able to display plots during training so I can see metrics directly in my notebook (updated once per epoch).

Is there any way to get rich content to display from within a remote function? Or do I need to pass data back some other way? Or, do I need to bite the bullet and emit metrics to a remote service?

@mwaskom
Copy link
Contributor

mwaskom commented Feb 25, 2025

Hi, the logging that goes back to the local client only includes the stdout/stderr pipes; we don't capture matplotlib figures. You'll need to return from the Function to show the figure. Here's an example that works:

Image

@mwaskom mwaskom changed the title Showing output in Jupyter notebooks Capturing matplotlib figures in Jupyter notebooks Feb 25, 2025
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

2 participants