sagemathinc / cocalc

CoCalc: Collaborative Calculation in the Cloud
https://CoCalc.com
Other
1.17k stars 216 forks source link

Notebooks prefer latex or plain text output over html #5925

Closed iimog closed 2 years ago

iimog commented 2 years ago

When using notebooks with the julia or R kernels output is generated with multiple mime types (text/html, text/latex, text/markdown, text/plain). In both kernels, html used to be the default for display. This way, the output (particularly tables) looked great in the browser. Now in julia notebooks, latex seems to have higher priority than html, which does not look nice. When I force the IJulia kernel to not produce latex output, I get the html rendered. See screenshot: screenshot_julia

When I use R, plain text seems to be the preferred output in any case. As seen when I just switch the kernel to R: screenshot_R

I think, this is related to #5764 and the changes were introduced in https://github.com/sagemathinc/cocalc/commit/e0e10a2482bd0fa72eb032305457c6e51fdb4094

It would be great to get the old rendering behavior (preferring html for both R and julia) back.

williamstein commented 2 years ago

Please include actual code in triple back ticks or links to notebooks instead of screenshots, since otherwise I will literally have to retype everything in your examples above just to test it. That means, of course, that I won't test it.

williamstein commented 2 years ago

I'll fix this today in any case, as follows:

williamstein commented 2 years ago

OK, a quick observation is that Julia uses the tabular latex environment. This is something that has absolutely nothing to do with KaTeX or MathJax -- it's not part of math mode in latex itself. It's something that can only be meaningfully rendered using an actual latex engine. So clearly the text/latex output from Julia is only ever meant to be used by nbconvert on the backend, and simply can't be meaningful in a purely frontend context (without a full latex compiler).

So for nice latex-style output, one needs either to run latex on the backend, to use something massive like https://www.swiftlatex.com/ (latex in web assembly), or to (slightly) change the Julia and R kernels to generate latex that can be used both on the frontend and in nbconvert. I won't do any of those things any time soon.

iimog commented 2 years ago

Thanks a lot @williamstein for looking into this so quickly. Here is the code for testing:

using DataFrames
DataFrame(a=[1,2],b=[2,3])

The notebook is here.

I totally get that any heavy changes to the latex engine are out of scope for this issue. I hope there is a more pragmatic solution for now. Anyway, I appreciate your work and support, and I love using Cocalc.

williamstein commented 2 years ago

Thanks for your encouragement and code!

Regarding latex, I do think it might be best to fully properly render latex output -- as an option for users, just because it can look very nice. I did work improving our pdfjs integration recently, and have been thinking about maybe using that -- so latex would really get run and displayed nicely using pdfjs (which is a pdf viewer implemented in javascript). That's how %latex in Sage worksheets works, except that used a png image, and how the old %latex worked in the 2007 Sage notebook (also a png image). pdfjs would look much better.

williamstein commented 2 years ago

Hi, These changes will be live in an hour or two, and impact both the app and https://cocalc.com/share

Note that a hard constraint with https://cocalc.com/share is that anything there be 100% safe against XSS attacks, so for there we are rendering plain text instead of HTML for Julia and R, since plain text is much better than HTML after sanitization. It would be nice to render text/latex (which can be done safely), but unfortunately using {tabular} just doesn't really work yet.

Once this is live, let me know if you run into other situations where mime type priorities should be changed.

iimog commented 2 years ago

Awesome! :tada: Thank you so much. I can confirm that tables are nicely displayed again for both Julia and R. Prioritizing security for shared notebooks and thus falling back to plain text there, makes a lot of sense.

williamstein commented 2 years ago

Prioritizing security for shared notebooks and thus falling back to plain text there, makes a lot of sense.

Thanks. I view this is temporary, of course... security first, but then do something better (still not sure what in this case though).

williamstein commented 1 week ago

For the Sage kernel, the latex output is much better than the HTML, since it outputs this:

<html>\(\displaystyle \frac{2}{3}\)</html>

and isn't supposed to be in the DOM. So annoyingly, which is best depends on the kernel...