Rich cell output with Jupyter Notebook (IPython)

Do you guys use SymPy? When I use SymPy with Jupyter Notebook, the output formula is beautifully shaped and displayed. image.png Something like this. Do you know how to create an object that produces such rich output?

It was written in IPython documentation. This can be achieved by implementing a method with a name of the form _repr_ * _. What applies to this * is

there is. I think SymPy is probably implemented in LaTeX, but this time let's use SVG, which is easy to understand.

Face() + Eye(35, 40) + Eye(65, 40) + Mouth(50, 75)

image.png

The implementation is as follows.

class Component():
    def __init__(self, items = None):
        self.items = items or [self]
    def __add__(self, target):
        return Component(self.items + target.items)
    def _repr_svg_(self):
        return ''.join([
            '<svg width="100" height="100">',
            *map(str, self.items),
            '</svg>'
        ])
    
class Face(Component):
    def __str__(self):
        return '<circle cx="50" cy="50" r="45" fill="none" stroke="black" stroke-width="1" />'

class Eye(Component):
    def __init__(self, x, y):
        super().__init__()
        self.x, self.y = x, y
    def __str__(self):
        return f'<circle cx="{self.x}" cy="{self.y}" r="5" fill="black" />'

class Mouth(Component):
    def __init__(self, x, y):
        super().__init__()
        self.x, self.y = x, y
    def __str__(self):
        return f'<path d="M{self.x} {self.y} l15 -5 h-30 Z" fill="none" stroke="black" stroke-width="1" />'

Recommended Posts

Rich cell output with Jupyter Notebook (IPython)
Play with Jupyter Notebook (IPython Notebook)
Use apache Spark with jupyter notebook (IPython notebook)
How to debug with Jupyter or iPython Notebook
When Html cannot be output with Jupyter Notebook
Graph drawing with jupyter (ipython notebook) + matplotlib + vagrant
Parallel computing with iPython notebook
Using Graphviz with Jupyter Notebook
Use pip with Jupyter Notebook
To output a value even in the middle of a cell with Jupyter Notebook
EC2 provisioning with Vagrant + Jupyter (IPython Notebook) on Docker
Use Cython with Jupyter Notebook
Graph drawing with IPython Notebook
Use Bokeh with IPython Notebook
Allow external connections with jupyter notebook
R & D life with iPython notebook
Formatting with autopep8 on Jupyter notebook
Visualize decision trees with jupyter notebook
Make a sound with Jupyter notebook
Add more kernels with Jupyter Notebook
Build IPython Notebook environment with boot2docker
Convenient analysis with Pandas + Jupyter notebook
Use nb extensions with Anaconda's Jupyter notebook
Use Jupyter Lab and Jupyter Notebook with EC2
Try SVM with scikit-learn on Jupyter Notebook
How to use jupyter notebook with ABCI
Linking python and JavaScript with jupyter notebook
"LIVE" HTML presentation with IPython 3.0.0-dev, IPython Notebook
[Jupyter Notebook memo] Display kanji with matplotlib
Jupyter Notebook memo
Introducing Jupyter Notebook
Analytical environment construction with Docker (jupyter notebook + PostgreSQL)
Data analysis for improving POG 2 ~ Analysis with jupyter notebook ~
Verify NLC accuracy with Watson Studio's Jupyter Notebook
Powerful Jupyter Notebook
Enable Jupyter Notebook with conda on remote server
Jupyter Notebook memo
ipython notebook installation
IPython Notebook Recommendations
R environment construction with Jupyter (formerly IPython notebook) (on OS X El Capitan 10.11.3)
Create a table of contents with IPython notebook
Virtual environment construction with Docker + Flask (Python) + Jupyter notebook
Easy Machine Learning with AutoAI (Part 4) Jupyter Notebook Edition
Monitor the training model with TensorBord on Jupyter Notebook
Data analysis environment construction with Python (IPython notebook + Pandas)
Import specific cells from other notebooks with Jupyter notebook
Multiple selections with Jupyter
Candlestick with plotly + Jupyter
Get started Jupyter Notebook
Output PDF with Django
Markdown output with Django
Output PDF with WeasyPrint
Jupyter is coming in with great momentum, so be prepared for you (IPython Notebook + R)
Start IPython with virtualenv
Make slides with iPython
Remotely connect IPython notebook
Jupyter cell move shortcut
Use nim with Jupyter
[Cloud103] # 3 Jupyter Notebook again
[Machine learning] Start Spark with iPython Notebook and try MLlib
I want to use R functions easily with ipython notebook