JuliaLang / IJulia.jl

Julia kernel for Jupyter
MIT License
2.78k stars 409 forks source link

Not seeing any output after running plotly() with julia 1.9 #1073

Closed bryaan closed 1 year ago

bryaan commented 1 year ago

Jupyter isn't outputting anything in the cells with the julia 1.9-rc3 kernel. Please fix.

bryaan commented 1 year ago

Upon further review this is happening after running plotly() as in

using Plots
plotly()
plot(x, y)

Filing an issue with Plots.jl

ajw-free commented 1 year ago

I have the same problem with the Julia 1.9 release.
My platform is Ubuntu Mate 23.04 and Firefox browser.

If I load Jupyter notebook, then use Plots with plotly() backend, the code below does not display the plot object.

using Plots
plotly()
display(plot(rand(10)))

If I use the gr() backend, the plot image does display in the noteboook.

I should add that if I use Plots with plotly() from the command line REPL, it does work - a tab on my browser opens and displays the plot.

Please fix.

ajw-free commented 1 year ago

I compared the html code created by Jupyter Notebook (which displays nothing) with the html created from the REPL (which display properly in a browser tab).

To do this, I used savefig() to save to an html file which can be viewed in an editor.

There are differences in the first few lines which suggest that some header information is not being written.

(For gr() the files are the same, as expected.)

To see the differences:

(1) Run this following code via the REPL: using Plots plotly() p=plot(1:10) savefig(p, "plot_from_REPL.html") display(p) # works and show plot in a browser tab.

(2) Run this code via Jupyter Notebook: using Plots plotly() p=plot(1:10) savefig(p, "plot_from_jupyter.html") display(p) # shows no display.

RESULTS

(1) File "plot_from_REPL.html" starts with: image etc.

(2) File "plot_from_jupyter.html" starts immediately with: image etc.

Clearly text is missing at the start of the html created from within the notebook.

I hope that someone can soon be allocated to fix this bug!

ajw-free commented 1 year ago

As a temporary solution, one can install and use the plotlyjs() backend instead of plotly().
It works with Julia 1.9.0 and Jupyter Notebook, and allows interactive plots.

using Pkg Pkg.add("PlotlyJS")

From Jupyter notebook: using Plots plotlyjs() plot(rand(10))

a15355447898a commented 1 year ago

As a temporary solution, one can install and use the plotlyjs() backend instead of plotly(). It works with Julia 1.9.0 and Jupyter Notebook, and allows interactive plots.

using Pkg Pkg.add("PlotlyJS")

From Jupyter notebook: using Plots plotlyjs() plot(rand(10))

I tried to run this, and then, my jupyter notebook kept running without any return value

using Plots
plotlyjs()
plot(rand(10))
image

The image was not returned until I closed the jupyter kernel

image


Then I tried to use GR as the backend, and I only got the base64 data of the image

using Plots
Plots.gr()
Plots.plot(Plots.fakedata(50, 5), w=3)
image

Finally I found that using IJulia.display can output images properly

using Images
image = [RGB(rand(N0f8,3)...) for x in 1:200, y in 1:150]
using IJulia
IJulia.display(image)
image
stevengj commented 1 year ago

IJulia.display is the same as Base.display, which is the same as what is called on the result of evaluating a cell. So it should make no difference. Indeed, your example works just fine for me without any explicit display call in Julia 1.9 on Mac (M4):

image
stevengj commented 1 year ago

If there is a problem with plotly, maybe report the error there? It's not clear to me that the problem is with IJulia.

stevengj commented 1 year ago

Plots.gr() works fine for me:

image
stevengj commented 1 year ago

It looks like the problem with Plotly is that its output MIME type "application/vnd.plotly.v1+json" is not registered as text, so it is getting base-64 encoded even though it should be sent as JSON text. This is causing an error on the Python side when it tries to decode the JSON (it aborts with an error because the base64 stuff is not valid JSON).

I can fix it by defining:

Base.istextmime(::MIME"application/vnd.plotly.v1+json") = true

which then gives:

image

but it really seems like this should be defined in Plotly.jl. (This istextmime method must have been defined at one point for it to work, no? Was it removed recently?)

a15355447898a commented 1 year ago

It looks like the problem with Plotly is that its output MIME type "application/vnd.plotly.v1+json" is not registered as text, so it is getting base-64 encoded even though it should be sent as JSON text. This is causing an error on the Python side when it tries to decode the JSON (it aborts with an error because the base64 stuff is not valid JSON).

I can fix it by defining:

Base.istextmime(::MIME"application/vnd.plotly.v1+json") = true

which then gives: image

but it really seems like this should be defined in Plotly.jl. (This istextmime method must have been defined at one point for it to work, no? Was it removed recently?)

The sad thing is that I still have the problem here, which happens on top of vscode's jupyter

image
using Plots
plotlyjs()
plot(rand(10))

image
using Plots
Plots.gr()
Plots.plot(Plots.fakedata(50, 5), w=3)
┌ Error: Some Julia code in the VS Code extension crashed
└ @ VSCodeServer [c:\Users\](file:///C:/Users/)孙文彬\.vscode\extensions\julialang.language-julia-1.47.2\scripts\error_handler.jl:15
ERROR: UndefVarError: `Base64EncodePipe` not defined
Stacktrace:
  [1] send_stream(name::String, send_callback::typeof(VSCodeServer.io_send_callback))
    @ VSCodeServer.IJuliaCore [c:\Users\](file:///C:/Users/)孙文彬\.vscode\extensions\julialang.language-julia-1.47.2\scripts\packages\IJuliaCore\src\stdio.jl:152
  [2] flush(io::VSCodeServer.IJuliaCore.IJuliaStdio{Base.PipeEndpoint, typeof(VSCodeServer.io_send_callback)})
    @ VSCodeServer.IJuliaCore [c:\Users\](file:///C:/Users/)孙文彬\.vscode\extensions\julialang.language-julia-1.47.2\scripts\packages\IJuliaCore\src\stdio.jl:272
  [3] flush_all()
    @ VSCodeServer.IJuliaCore [c:\Users\](file:///C:/Users/)孙文彬\.vscode\extensions\julialang.language-julia-1.47.2\scripts\packages\IJuliaCore\src\stdio.jl:255
  [4] (::VSCodeServer.var"#198#199"{VSCodeServer.NotebookRunCellArguments, String})()
    @ VSCodeServer [c:\Users\](file:///C:/Users/)孙文彬\.vscode\extensions\julialang.language-julia-1.47.2\scripts\packages\VSCodeServer\src\serve_notebook.jl:65
  [5] withpath(f::VSCodeServer.var"#198#199"{VSCodeServer.NotebookRunCellArguments, String}, path::String)
    @ VSCodeServer [c:\Users\](file:///C:/Users/)孙文彬\.vscode\extensions\julialang.language-julia-1.47.2\scripts\packages\VSCodeServer\src\repl.jl:249
  [6] notebook_runcell_request(conn::VSCodeServer.JSONRPC.JSONRPCEndpoint{Base.PipeEndpoint, Base.PipeEndpoint}, params::VSCodeServer.NotebookRunCellArguments)
    @ VSCodeServer [c:\Users\](file:///C:/Users/)孙文彬\.vscode\extensions\julialang.language-julia-1.47.2\scripts\packages\VSCodeServer\src\serve_notebook.jl:13
  [7] dispatch_msg(x::VSCodeServer.JSONRPC.JSONRPCEndpoint{Base.PipeEndpoint, Base.PipeEndpoint}, dispatcher::VSCodeServer.JSONRPC.MsgDispatcher, msg::Dict{String, Any})
    @ VSCodeServer.JSONRPC [c:\Users\](file:///C:/Users/)孙文彬\.vscode\extensions\julialang.language-julia-1.47.2\scripts\packages\JSONRPC\src\typed.jl:67
  [8] serve_notebook(pipename::String, outputchannel_logger::Base.CoreLogging.SimpleLogger; crashreporting_pipename::String)
    @ VSCodeServer [c:\Users\](file:///C:/Users/)孙文彬\.vscode\extensions\julialang.language-julia-1.47.2\scripts\packages\VSCodeServer\src\serve_notebook.jl:139
  [9] top-level scope
    @ [c:\Users\](file:///C:/Users/)孙文彬\.vscode\extensions\julialang.language-julia-1.47.2\scripts\notebook\notebook.jl:32
 [10] include(mod::Module, _path::String)
    @ Base [.\](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/%E5%AD%99%E6%96%87%E5%BD%AC/Downloads/%E4%BD%BF%E7%94%A8julia%E5%92%8Cpython%E9%87%8D%E6%96%B0%E8%AE%A1%E7%AE%97%E5%A4%AA%E9%98%B3%E9%AB%98%E5%BA%A6%E8%A7%92%E9%97%AE%E9%A2%98/)Base.jl:457
 [11] exec_options(opts::Base.JLOptions)
    @ Base [.\](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/%E5%AD%99%E6%96%87%E5%BD%AC/Downloads/%E4%BD%BF%E7%94%A8julia%E5%92%8Cpython%E9%87%8D%E6%96%B0%E8%AE%A1%E7%AE%97%E5%A4%AA%E9%98%B3%E9%AB%98%E5%BA%A6%E8%A7%92%E9%97%AE%E9%A2%98/)client.jl:307
 [12] _start()
    @ Base [.\](https://file+.vscode-resource.vscode-cdn.net/c%3A/Users/%E5%AD%99%E6%96%87%E5%BD%AC/Downloads/%E4%BD%BF%E7%94%A8julia%E5%92%8Cpython%E9%87%8D%E6%96%B0%E8%AE%A1%E7%AE%97%E5%A4%AA%E9%98%B3%E9%AB%98%E5%BA%A6%E8%A7%92%E9%97%AE%E9%A2%98/)

image
using Plots
Plots.plotly()
Plots.plot(Plots.fakedata(50, 5), w=3)

image
Base.istextmime(::MIME"application/vnd.plotly.v1+json") = true

using Plots
Plots.plotly()
Plots.plot(Plots.fakedata(50, 5), w=3)
stevengj commented 1 year ago

Tried the Base.istextmime(::MIME "application/vnd.plotly.v1+json") = true that you mentioned, and the above code successfully returned the image

i.e. you're having the problem I identified above, which is solved in the latest release (1.24.2). Have you tried updating IJulia?

a15355447898a commented 1 year ago

Tried the Base.istextmime(::MIME "application/vnd.plotly.v1+json") = true that you mentioned, and the above code successfully returned the image

i.e. you're having the problem I identified above, which is solved in the latest release (1.24.2). Have you tried updating IJulia?

I have confirmed that I have upgraded IJulia to 1.24.2

image

a15355447898a commented 1 year ago

Tried the Base.istextmime(::MIME "application/vnd.plotly.v1+json") = true that you mentioned, and the above code successfully returned the image

i.e. you're having the problem I identified above, which is solved in the latest release (1.24.2). Have you tried updating IJulia?

I have confirmed that I have upgraded IJulia to 1.24.2

image

Just suddenly found that the image in the jupyter notebook normal display out, but in the vscode jupyter will not display out

image
stevengj commented 1 year ago

but in the vscode jupyter will not display out

Maybe you need to restart vscode?

ajw-free commented 1 year ago

stevengj - thanks for fixing this bug.

IJulia + Plots + plotly() backend now works again for me after updating Ijulia from v1.24.0 to v1.24.2.

ajw-free commented 1 year ago

This bug is currently present on MS Windows (tested 6 Sep 2023), but not on Linux.

Plotly backend does not display a plot in Jupyter notebook.

I tested on three PCs, running MS Windows 10, MS Windows 11, and Ubuntu Linux.

I did a fresh install of Julia 1.9.3 with added packages: IJulia v1.24.2 and Plots v1.39.0

On MS Windows, no output is shown within Jupyter notebook if I execute:

using Plots plotly() display(plot(rand(10)))

Oddly, on my Ubuntu Linux installation, running the same version of Julia 1.9.3 and updated packages, it correctly displays the plot. This bug needs to be reopened and fixed.

stevengj commented 1 year ago

The old bug was fixed, I think. If this is a new bug, it should be a new issue. But it's hard to tell from your description what is now causing the problem for you.

What is the value of keys(IJulia.display_dict(display(plot(rand(10)))))? This is what MIME types it is trying to display.

ajw-free commented 1 year ago

What is the value of keys(IJulia.display_dict(display(plot(rand(10)))))? This is what MIME types it is trying to display.

On Windows 11 (which shows no plot display, just a blank space) I get:

KeySet for a Dict{String, Union{String, JSON.Writer.JSONText}} with 1 entry. Keys: "text/plain"

On my Ubuntu Linux, which does display the plot, the keys(...) output gives the same text as Windows: KeySet for a Dict{String, Union{String, JSON.Writer.JSONText}} with 1 entry. Keys: "text/plain"

ajw-free commented 1 year ago

I do not know if the original problem was ever solved for Windows installations, or whether it is a new issue affecting only Windows. Looking back on the bug reports above, nobody mentions Windows, so perhaps the bug was not ever solved for Windows.

ajw-free commented 12 months ago

@stevengj I have opened a new issue: "No plot output from Plots with plotly() backend on Julia (on Windows and Mac)" https://github.com/JuliaLang/IJulia.jl/issues/1094 The issue is present in other versions of Julia. I have tested Julia v1.9.3 and LTS v1.6.7 and v1.10.0 beta 3.

The same problem is present in VS code's notebook environment. This leads me to conclude that the current problem is not within the IJulia code. It is probably within the Plots code.
I have reported it here: https://github.com/JuliaPlots/Plots.jl/issues/4827