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#!/usr/bin/env python3
 2# -*- coding: utf-8 -*-
 3"""
 4Main module for working with SLiCAP from within Jupyter Notebook or Jupyter Lab.
 5
 6This module imports specific modules for rendering HTML and SVG in notebooks. It
 7also sets the configuration variable *ini.notebook* that provides correct latex
 8output from the functions in the module **SLiCAPhtml.py**.
 9
10It also activates a work around for rendering reStructured text (.rst files)
11in notebooks.
12"""
13
14from IPython.core.display import HTML, SVG
15from IPython.core.magic import register_cell_magic, register_line_magic
16from IPython.display import Image
17
18# Work around for displaying rst in jupyter notebooks
19
20@register_cell_magic
21def rst(line, cell):
22    """
23    Render ReStructuredText:
24        
25        %%rst
26        ============
27         Main title
28        ============
29    """
30    writer = docutils.writers.html5_polyglot.Writer()
31    return HTML(docutils.core.publish_string(cell, writer=writer).decode('UTF-8'))
32
33@register_line_magic
34def rstfile(filename):
35    """
36    Render ReStructuredText file:
37    
38        %rstfile <file name>
39    """
40    writer = docutils.writers.html5_polyglot.Writer()
41    with open(filename, 'r') as file:
42        cell = file.read()
43    return HTML(docutils.core.publish_string(cell, writer=writer).decode('UTF-8'))
44