Jupyter Notebook formatting in Pressbooks
February 29, 2024 8:56 AM   Subscribe

Technical Question: I have a series of Jupyter Notebooks (.ipynb files) that I would like to import into a Pressbooks page. For now, I've been converting files to html, and importing them into Pressbooks that way. This works, though dealing with any image files is tricky, however the formatting is lost.

How can I preserve the look and feel of the notebook, once it's in Pressbooks? Things like the colour of the text, the boxes around the code blocks, ect. Things I have tried:
* Converting using most of the options on pandoc
* converting with Quarto
* Converting to markdown then to html (best way for images, loses all formatting)
* Convert to an Rmd, and use knittr. This had trouble parsing the Python
* Asking ChatGPT how to do it - this was a bust.

I think the answer is to create a custom CSS in Pressbooks, but this is outside of my wheelhouse. I know basic html, but my website making days never included CSS.
posted by Valancy Rachel to Technology (3 answers total) 1 user marked this as a favorite
 
Best answer: Screenshots?

Alternatively. You don't need to write CSS from scratch. Jupyter embeds all the required CSS in its HTML exports. So view source of your export, and copy the inner contents of all the <style type="text/css"></style> tags. I count 9 style tags on my Jupyter setup. The CSS is probably the same on every export, so you only have to do this once. Then paste all of that into Pressbooks custom css.

If you're also copying the HTML out of the source of your exported notebooks, it should all tie up. I don't have Pressbooks access to test this and iron out the inevitable issues but in principle it should work.
posted by Klipspringer at 9:49 AM on February 29, 2024


Best answer: Can you list the options in the Import Type dropdown in Pressbooks? The docs say,
EPUB (.epub)
Word document (.docx)
OpenOffice documents (.odt)
Pressbooks/WordPress XML (.xml or .wxr)
Web page (.html or URL)
are those all?

Looks like Jupyter expects to export through a powerful but alas huge chain of tools, viz. nbconvert pandoc LaTex, and I'm hunting around for what looks like the shortest, most systematic path from nbconvert outputs to Pressbooks inputs. If there aren't more input formats lurking in Pressbooks I would start by going through EPUB, since it's *for* books:

https://stackoverflow.com/questions/3588373/convert-latex-to-epub

For people who don't use the commandline much: this is likely to be a at-best-tedious hour of installing things at the commandline, and then possibly some hassle tuning the options you want when exporting/importing, and then you put those tuned commands into a little file you can copy/paste out of and it's blazing fast after that.
posted by clew at 11:11 AM on February 29, 2024


Response by poster: Klipspringer: You are completely correct. I copied the CSS out of an exported html file, and everything looks exactly the way I need. Thanks!
posted by Valancy Rachel at 11:11 AM on February 29, 2024 [1 favorite]


« Older They say it's your birthday...   |   What foods did you notice in this year's... Newer »

You are not logged in, either login or create an account to post comments