4

I'm trying to render Jupyter markdown cells with code blocks inside HTML alert boxes, but can't get the syntax highlighter to work for both reveal.js slides and notebook cell output.

Edit: I'm using JupyterLab 2.0 to render the notebooks (but behaves the same in classic Jupyter Notebook) and $ jupyter nbconvert --to slides <notebook>.ipynb to create the RevealJS <notebook>.slides.html output.

For instance, this markdown cell code works well in Jupyter, but is malformatted in RevealJS slides:

<div class="alert alert-block alert-info" style="background-color: white; border: 2px solid; padding: 10px">
    <b><i class="fa fa-info-circle" aria-hidden="true"></i>&nbsp; Info</b><br>
    <p style="color: black">
        We can also specify the order of the words within a sentence using numbers within the curly braces:
    </p>
<div>

```python
>>> "Hello {1} {0}".format("Mr.", "Bond")  # explicit argument order
'Hello Bond Mr.'
```

</div>
</div>

Jupyter markdown rendering: enter image description here

reveal.js version: enter image description here

Attempt 2

Alternatively, the following displays the code correctly (minus the syntax highlighting) for both the markdown cell and the reveal.js slides output:

<div class="alert alert-block alert-info" style="background-color: white; border: 2px solid; padding: 10px">
    <b><i class="fa fa-info-circle" aria-hidden="true"></i>&nbsp; Info</b><br>
    <p style="color: black">
        We can also specify the order of the words within a sentence using numbers within the curly braces:
    </p>
<div>

<pre><code>>>>"Hello {1} {0}".format("Mr.", "Bond")  # explicit argument order
'Hello Bond Mr.'
</code></pre>

</div>
</div>

Rendering for both: Jupyter, no syntax highlighting

Attempt 3

I also tried using using syntax highlighting solutions, such as code-prettify, which while producing highlighted output in reveal.js (nevermind it not matching the rest of the code style) did not help with syntax highlighting within the Jupyter notebook (in JupyterLab at least). I've included an example of this failed attempt to document my thoughts...

<div class="alert alert-block alert-info" style="background-color: white; border: 2px solid; padding: 10px">
    <b><i class="fa fa-info-circle" aria-hidden="true"></i>&nbsp; Info</b><br>
    <p style="color: black">
        We can also specify the order of the words within a sentence using numbers within the curly braces:
    </p>
<div>

<script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>

<!-- language-all: lang-python -->
  
<pre class="prettyprint lang-python"><code class="language-python">>>>"Hello {1} {0}".format("Mr.", "Bond")  # explicit argument order
'Hello Bond Mr.'
</code></pre>

</div>
</div>

Attempt 4

Finally, I tried opening the outputted slides.html file produced by nbconvert and trying to make sense of how the syntax highlighting works... It didn't go well! In theory, this may be a bug with the conversion tool as the outputted HTML looks like this (with the code backticks escaped so that SO renders it okay):

<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<div class="alert alert-block alert-info" style="background-color: white; border: 2px solid; padding: 10px">
    <b><i class="fa fa-info-circle" aria-hidden="true"></i>&nbsp; Info</b><br>
    <p style="color: black">
        We can also specify the order of the words within a sentence using numbers within the curly braces:
    </p>
<div>

```python
>>> "Hello {1} {0}".format("Mr.", "Bond")  # explicit argument order
'Hello Bond Mr.'
```

</div>
</div>
</div>
</div>
</div>

If anyone knows of a way to solve this problem, then I would love to hear from you! I've lost many hours, my wits and my patience over this issue - please help!

Liam Deacon
  • 904
  • 1
  • 9
  • 25
  • I'm not yet clear on how you convert to HTML and SlideJS. Can you describe in more detail? – tarleb Jun 24 '20 at 20:29
  • Ah - good point! I'm using Jupyter's nbconvert command e.g. `jupyter nbconvert --to slides .ipynb`, but have also tried using the JupyterLab UI: `File -> Export Notebook As -> Export Notebook to RevealJS Slides` – Liam Deacon Jun 26 '20 at 09:56
  • I'm running into this as well, what I'm noticing is that `nbconvert` is treaing the backticked code block as a trigger to enter LaTeX MathMode, causing MathJax renderings to show up and ruin everything. Here is a simple, frustrating example: ```
    \`\`\`python a = 1 px.line(x) \`\`\`
    ``` produces: `pythona=1px.l∈e(x)`
    – Lucas Durand Apr 17 '21 at 01:11

0 Answers0