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

Add support for displaying image/png data. #406

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

teeler
Copy link

@teeler teeler commented Dec 13, 2024

This change maps image/png display data from execution results as tags with in-line data.

Side note - Hi! I'm Tyler, I work on Colab at Google. Huge fans of this project - we're using it to show folks how to deploy Colab notebooks on Cloud Run with the kernel gateway (https://x.com/GoogleColab/status/1864765949913739725).

I added this bit of functionality so users could display simple (and only png) objects in notebooks, for example this pair of cells:

# ResponseInfo GET /plot
print(
    json.dumps(
        {
            "headers": {"Content-Type": "text/html"},
            "status": 200,
        }
    )
)

And

# GET /plot

# prompt: Generate a random array with numpy and plot it (thanks Gemini!)

import numpy as np
import matplotlib.pyplot as plt

# Generate a random array
random_array = np.random.rand(10)

# Plot the array
plt.plot(random_array)
plt.title("Random Array Plot")
plt.xlabel("Index")
plt.ylabel("Value")
plt.show()

Produces correct output for /plot

Screenshot 2024-12-13 at 3 17 31 PM

as an in-line <img> tag.

<img alt="<Figure size 640x480 with 1 Axes>" src="data:image/png;base64,iVBORw....." >

It also has the capability of mixing between images/html/text.

Happy to re-arrange/format/etc. any of this code, thanks for reading!

This change maps image/png display data from execution results as <img> tags with in-line data.
@teeler
Copy link
Author

teeler commented Dec 19, 2024

@parente (sorry for tagging you directly) - I'd be happy to fix up some of these tests in exchange for some feedback on this PR. ;)

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

Successfully merging this pull request may close these issues.

1 participant