quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.74k stars 305 forks source link

Render Plotly Interactive Plot in Colab and Quarto #10263

Open s2t2 opened 1 month ago

s2t2 commented 1 month ago

Bug description

I have an IPYNB file (notebook downloaded from Google Colab). It has a cell in it that makes a chart in plotly:

import plotly.express as px

fig = px.line(x=[1,2,3], y=[1,2,3])
fig.show()

I see the chart in the colab notebook, but not when I render that notebook as a source file for a Quarto project.

Plotly figures do get displayed when rendered from a .QMD file, but I want to use .IPYNB because my notebook has taken advantage of Colab-specific functionality, like forms, notebook secrets, etc.

My question is: how can I get plotly figures to be properly displayed in Quarto, using a .IPYNB file source, in such a way that will work BOTH in colab and in Quarto?

Here is some investigation code. It appears different renderers work on colab vs quarto, but there is no overlap?

The solution documented here does not render in colab: https://quarto.org/docs/interactive/widgets/jupyter.html#plotly

pio.renderers.default = "colab+plotly_mimetype+notebook_connected"
fig.show()

Here is a notebook that shows the results of all renderer options in Colab. Only "colab" and "sphinx_gallery" renderers work in Colab. But they don't work in Quarto. We need one option that will work for both.

https://colab.research.google.com/drive/1UpmfA_Ybj8i4rDtgPnQz1af79STG2enP?usp=sharing

https://stackoverflow.com/questions/78694049/plotly-figures-not-displaying-in-quarto

Steps to reproduce

I have an IPYNB file (notebook downloaded from Google Colab). It has a cell in it that makes a chart in plotly:

import plotly.express as px

fig = px.line(x=[1,2,3], y=[1,2,3])
fig.show()

I see the chart in the colab notebook, but not when I render that notebook as a source file for a Quarto project.

Expected behavior

No response

Actual behavior

No response

Your environment

No response

Quarto check output

(quarto-env) --->> quarto check Quarto 1.4.554 [✓] Checking versions of quarto binary dependencies... Pandoc version 3.1.11: OK Dart Sass version 1.69.5: OK Deno version 1.37.2: OK [✓] Checking versions of quarto dependencies......OK [✓] Checking Quarto installation......OK Version: 1.4.554 Path: /Applications/quarto/bin

[✓] Checking tools....................OK TinyTeX: (not installed) Chromium: (not installed)

[✓] Checking LaTeX....................OK Using: Installation From Path Path: /usr/local/bin Version: undefined

[✓] Checking basic markdown render....OK

[✓] Checking Python 3 installation....OK Version: 3.11.9 (Conda) Path: /opt/anaconda3/envs/quarto-env/bin/python Jupyter: 5.7.2 Kernels: python3

[✓] Checking Jupyter engine render....OK

[✓] Checking R installation...........OK Version: 4.3.1 Path: /usr/local/Cellar/r/4.3.1/lib/R LibPaths:

cscheid commented 1 month ago

If the solution we document works for Quarto, then I think we don't consider this a bug. It would be nice to learn of a solution that's portable to other execution environments, but that's an enhancement.

cderv commented 1 month ago

I have an IPYNB file (notebook downloaded from Google Colab). It has a cell in it that makes a chart in plotly:

import plotly.express as px

fig = px.line(x=[1,2,3], y=[1,2,3])
fig.show()

I see the chart in the colab notebook, but not when I render that notebook as a source file for a Quarto project.

FWIW we have other issues / questions about this renderers thing for plotly. About this, my understanding is that plotly will choose the appropriate renderers. From https://plotly.com/python/renderers/#setting-the-default-renderer I read

In many contexts, an appropriate renderer will be chosen automatically and you will not need to perform any additional configuration.

Colab is one of the context mention. If you are executing your notebook on collab, the rendering will be done considering this context. So probably, the ipynb require new execution with Quarto quarto render from-collab.ipynb --execute. Especially if specific renderers are used like Google Colab dedicated one.

ipynb is a special format that store execution result. I don't know if there is a solution where the execution result from plotly for colab and quarto can be the same. It seems to me plotly expect a renderer to be associated to different environment, unless a more common renderer is used which can be more portable (like svg for example).

Now, the ipynb downloaded from collab should be ok to be rendered with quarto render - if not, then this is another issue as Quarto should be able to execute any ipynb when the right python environment is provided.