Search code examples
jupyterkdbembedpy

Imaging plotly figure as image in jupyterq using embedpy


When I try t display a plotly graph from pyhton as image that works fine

import plotly.express as px
from IPython.display import Image

fig = px.scatter(x=range(10), y=range(10))
Image(fig.to_image(format="png"))

However, when I try the same via embedpy from jupyterq, the graph wont display

px:.p.import[`plotly.express]
ipd:.p.import[`IPython.display]

fig:px[`:scatter][`x pykw til 10; `y pykw til 10]
img:fig[`:to_image][`format pykw `png]
ipd[`:Image][img]

The graph wont display. I also tried actively displaying it

ipd[`:display][ipd[`:Image][img]]

But this just shows tiny generic image thumbnail

The regular plotly graph displays fine

fig[`:show][]

Also, trying to execute the python code via .p.e does not show the image, but just the thumbnail

.p.e["import plotly.express as px"]
.p.e["from IPython.display import Image, display"]
.p.e["fig=px.scatter(x=range(10), y=range(10))"]
.p.e["Image(fig.to_image(format='png'))"]
.p.e["display(Image(fig.to_image(format='png')))"]

Solution

  • The issue appears to be in .qpk.mc function. Two ways to fix this, provided by a co-worker:

    Either overriding the offending function

    .qpk.mc:{y}
    

    Inject into an html that can handle the encoded string

    ipd[`:display]ipd[`:HTML]["<img src=\"data:image/png;base64,",.b64.enc[img`],"\">"];