SciRuby / iruby

Official gem repository: Ruby kernel for Jupyter/IPython Notebook
https://github.com/SciRuby/iruby
MIT License
890 stars 27 forks source link

Javascript output is base64 encoded #292

Closed kylekyle closed 3 years ago

kylekyle commented 3 years ago

It looks like javascript output is base64 encoded:

IRuby.javascript("alert('hello world!');")

Which produces the following error:

Javascript error adding output!
SyntaxError: Unexpected end of input
See your browser Javascript console for more details.

Seems to be related to the Display#protect method.

kylekyle commented 3 years ago

Not sure if this is related, but the SUPPORTED_MIMES includes application/javascript, while the registered renderer produces text/javascript.

azriel1rf commented 3 years ago

I have a same problem.

a = IRuby.input 'a'
Javascript error adding output!
SyntaxError: Unexpected end of input
See your browser Javascript console for more details.

Windows 10 Python 3.8.2 (installed with pyenv) jupyter core 4.7.0 jupyter-notebook 6.2.0 (installed with pip) ruby 3.0.1p64 (installed with Scoop) irb 1.3.5


kojix2 commented 3 years ago

Hi @kylekyle and @azriel1rf

Thank you for your report. I confirmed that the bug was reproduced. As kylekyle pointed out, the javascript characters are converted into Base64 encoded forms.

IRuby::Display.display("a=0;", mime: "application/javascript")
# actual: {"text/plain"=>"\"a=0;\"", "application/javascript"=>"YT0wOw=="}
# expected: {"text/plain"=>"\"a=0;\"", "application/javascript"=>"a=0;"}

This behavior is caused by the recent replacement of mimemagic with mimetype. https://github.com/SciRuby/iruby/commit/230b9162cadb7e93659772624e85cb1fd9ca338e

mimemagic's text? methods and mimetype's ascii? method seems to work differently.

# irb -r mimemagic
MimeMagic.new("application/javascript").text?
# => true
# irb -r mime/types
MIME::Type.new("application/javascript").ascii?
# => false

This difference causes the protect method to fail.

What should we do, @mrkn ?

kojix2 commented 3 years ago

If anyone can make a pull request to fix this problem, please send it to us. I'll merge.

kojix2 commented 3 years ago

Thanks @mrkn. I checked (#294). A pop-up window appears.

image

@kylekyle is the author of IRuby Input. IRuby Input has not been updated recently. If you want to continue maintaining IRuby Input, you might want to join the iruby-developers. Having the right to commit if necessary can help you stay motivated.

kojix2 commented 3 years ago

@azriel1rf The input fields are now working. If you find any problems, please report the issue again. image

@kylekyle Feel free to send us a Pull Request to improve IRuby Input, and we'll merge it. If you need to make a lot of changes to IRuby Input and you think you need commit rights, please let us know.

Thanks.