Content with notebooks

You can also create content with Jupyter Notebooks. The content for the current page is containedin a Jupyter Notebook in the notebooks/ folder of the repository. This means that we can includecode blocks and their outputs, and export them to Jekyll markdown.

You can find the original notebook for this page at this address

Markdown + notebooks

As it is markdown, you can embed images, HTML, etc into your posts!

Jupyter notebooks - 图1

You an also $add{math}$ and mathblocksmath^{blocks} or {tex} \ \math blocks\end{align*} But make sure you \$Escape \$your \$dollar signs \$you want to keep!

Code blocks and image outputs

Jupyter Book will also embed your code blocks and output in your book.For example, here's some sample Matplotlib code:

  1. from matplotlib import rcParams, cycler
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. plt.ion()
  1. # Fixing random state for reproducibility
  2. np.random.seed(19680801)
  3.  
  4. N = 10
  5. data = [np.logspace(0, 1, 100) + np.random.randn(100) + ii for ii in range(N)]
  6. data = np.array(data).T
  7. cmap = plt.cm.coolwarm
  8. rcParams['axes.prop_cycle'] = cycler(color=cmap(np.linspace(0, 1, N)))
  9.  
  10.  
  11. from matplotlib.lines import Line2D
  12. custom_lines = [Line2D([0], [0], color=cmap(0.), lw=4),
  13. Line2D([0], [0], color=cmap(.5), lw=4),
  14. Line2D([0], [0], color=cmap(1.), lw=4)]
  15.  
  16. fig, ax = plt.subplots(figsize=(10, 5))
  17. lines = ax.plot(data)
  18. ax.legend(custom_lines, ['Cold', 'Medium', 'Hot']);

Jupyter notebooks - 图2

Note that the image above is captured and displayed by Jekyll.

Jupyter notebooks - 图3

You can also pop out content to the side!. For more information on how to do this,check out the customizing page layout page.

Removing content before publishing

You can also remove some content before publishing your book to the web. For example,in the original notebook thereused to be a cell below…

You can also remove only the code so that images and other output still show up.

Below we'll only display an image. It was generated with Python code in a cell,which you can see in the original notebook

  1. thisvariable = "this plot *will* show up in the textbook."
  2.  
  3. fig, ax = plt.subplots()
  4. x = np.random.randn(100)
  5. y = np.random.randn(100)
  6. ax.scatter(x, y, s=np.abs(x*100), c=x, cmap=plt.cm.coolwarm)
  7. ax.text(0, .5, thisvariable, fontsize=20, transform=ax.transAxes)
  8. ax.set_axis_off()

Jupyter notebooks - 图4

And here we'll only display a Pandas DataFrame. Again, this was generated with Python codefrom this original notebook.

  1. import pandas as pd
  2. pd.DataFrame([['hi', 'there'], ['this', 'is'], ['a', 'DataFrame']], columns=['Word A', 'Word B'])
Word AWord B
0hithere
1thisis
2aDataFrame

You can configure the text that Textbooks with Jupyter uses for this by modifying your book's _config.yml file.

Interactive outputs

We can even do the same for interactive material. Below we'll display a map using ipyleaflet. When the notebookis converted to Markdown, the code for creating the interactive map is retained.

Note that this will only work for some packages. They need to be able to output standalone HTML/Javascript, and notdepend on an underlying Python kernel to work.

  1. import folium
  1. m = folium.Map(
  2. location=[45.372, -121.6972],
  3. zoom_start=12,
  4. tiles='Stamen Terrain'
  5. )
  6.  
  7. folium.Marker(
  8. location=[45.3288, -121.6625],
  9. popup='Mt. Hood Meadows',
  10. icon=folium.Icon(icon='cloud')
  11. ).add_to(m)
  12.  
  13. folium.Marker(
  14. location=[45.3311, -121.7113],
  15. popup='Timberline Lodge',
  16. icon=folium.Icon(color='green')
  17. ).add_to(m)
  18.  
  19. folium.Marker(
  20. location=[45.3300, -121.6823],
  21. popup='Some Other Location',
  22. icon=folium.Icon(color='red', icon='info-sign')
  23. ).add_to(m)
  24.  
  25.  
  26. m

Rich outputs from notebook cells

Because notebooks have rich text outputs, you can store these inyour Jupyter Book as well!

  1. !jupyter-book create -h
  1. usage: jupyter-book [-h] [--out-folder OUT_FOLDER] [--license LICENSE]
  2. [--content-folder CONTENT_FOLDER] [--toc TOC]
  3. [--config CONFIG] [--custom-css CUSTOM_CSS]
  4. [--custom-js CUSTOM_JS]
  5. [--extra-files EXTRA_FILES [EXTRA_FILES ...]]
  6. [--overwrite] [--demo] [--verbose VERBOSE]
  7. name
  8.  
  9. Create a new Jupyter Book
  10.  
  11. positional arguments:
  12. name The name of your Jupyter Book (your book template will
  13. be placed in a folder of this name)
  14.  
  15. optional arguments:
  16. -h, --help show this help message and exit
  17. --out-folder OUT_FOLDER
  18. The location where your book will be placed
  19. --license LICENSE A path to a LICENSE.md file if you have already
  20. created one
  21. --content-folder CONTENT_FOLDER
  22. A path to a folder that holds your book content
  23. --toc TOC A path to a yaml file that contains a Table of
  24. Contents for your Jupyter Book. This will overwrite
  25. parts of the book template's default toc.yml
  26. configuration
  27. --config CONFIG A path to a configuration YAML file that contains
  28. configuration for your Jupyter Book. This will
  29. overwrite parts of the book template's default
  30. _config.yml configuration
  31. --custom-css CUSTOM_CSS
  32. A path to a CSS file that defines some custom CSS
  33. rules for your book
  34. --custom-js CUSTOM_JS
  35. A path to a JS file that defines some custom CSS rules
  36. for your book
  37. --extra-files EXTRA_FILES [EXTRA_FILES ...]
  38. A list of extra files / folders to copy into your
  39. book's directory
  40. --overwrite Whether to overwrite a pre-existing book if it exists
  41. --demo Whether to build the book with demo content instead of
  42. your own content
  43. --verbose VERBOSE Whether to display output information. [yes/no]
  1. this_will_error
  1. ---------------------------------------------------------------------------
  2. NameError Traceback (most recent call last)
  3. <ipython-input-1-09f61459889d> in <module>()
  4. ----> 1 this_will_error
  5.  
  6. NameError: name 'this_will_error' is not defined

This page was created by The Jupyter Book Community