Creating block diagram using Python in Jupyter notebook
Creating block diagram¶
!pip install blockdiag
Diagram definition¶
First we create the folder:
!mkdir diagrams
and we create a new file inside the diagrams
folder with the definition of the graph:
%%file diagrams/block_diagram
blockdiag {
// Define class (list of attributes)
class emphasis [color = lightblue, style = dashed];
class redline [color = red, style = dotted];
Extract -> Transform -> Load;
// Set class to node
Transform [class = "emphasis"];
// Set class to edge
Transform -> Load [class = "redline"];
}
Convert the diagram to PNG¶
Using the blockdiag
library, we can convert the diagram from the definition to a PNG by running the blockdiag
command from the command line with the graph definition as first argument:
!blockdiag diagrams/block_diagram
Display the PNG¶
By using the display functionality of the Jupyter notebook we can show the generated PNG:
from IPython.display import Image
Image("diagrams/block_diagram.png")
Convert to slides¶
Jupyter has the functionality to convert a Jupyter notebook to a fancy looking presentation. By using nbconvert
and with the output set to slides
, the notebook will be translated to an HTML page, which will be transformed into a beautiful presentation by adding the RevealJS library.
!jupyter nbconvert creating_block_diagram.ipynb --to slides
Check for resulting HTML¶
After conversion we should see both the .ipynb and .html in the directory.
!ls -la creating_block_diagram.slides.html
Retrieve RevealJS¶
To use Jupyter slides we need to download RevealJS to the root directory of the notebooks. We do this by cloning the reveal.js
Git repository directly in the current folder.
!git clone https://github.com/hakimel/reveal.js.git
Start Python webserver¶
Use http.server
for Python 3 and SimpleHTTPServer
when using Python 2 to start the server to view the HTML page.
!python -m http.server 8000
Check port 8000 and open the HTML page containing the slides. In this case the HTML of this notebook will be visible on http://localhost:8000/creating_block_diagram.slides.html#/.