SLiCAPnotebook.pyΒΆ

SLiCAPnotebook is the main module to be imported when working with Jupyter notebooks. It imports extra modules for displaying RST, HTML and SVG in Jupyter notebooks.

It sets ini.notebook, which provides markdown output for rendering LaTeX in Jupyter notebooks.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Main module for working with SLiCAP from within Jupyter Notebook or Jupyter Lab.

This module imports specific modules for rendering HTML and SVG in notebooks. It
also sets the configuration variable *ini.notebook* that provides correct latex
output from the functions in the module **SLiCAPhtml.py**.

It also activates a work around for rendering reStructured text (.rst files)
in notebooks.
"""

from IPython.core.display import HTML, SVG
from IPython.core.magic import register_cell_magic, register_line_magic
from IPython.display import Image

# Work around for displaying rst in jupyter notebooks

@register_cell_magic
def rst(line, cell):
    """
    Render ReStructuredText:
        
        %%rst
        ============
         Main title
        ============
    """
    writer = docutils.writers.html5_polyglot.Writer()
    return HTML(docutils.core.publish_string(cell, writer=writer).decode('UTF-8'))

@register_line_magic
def rstfile(filename):
    """
    Render ReStructuredText file:
    
        %rstfile <file name>
    """
    writer = docutils.writers.html5_polyglot.Writer()
    with open(filename, 'r') as file:
        cell = file.read()
    return HTML(docutils.core.publish_string(cell, writer=writer).decode('UTF-8'))