Controlling the page layout

experimental

There are a few ways to control the layout of a page with Jupyter Book. Many of theseideas take inspiration from the Edward Tufte layout CSS guide.

Our plot

Let's begin with a sample plot. Here's the code we'll use:

  1. # Fixing random state for reproducibility
  2. np.random.seed(19680801)
  3.  
  4. N = 10
  5. data = [np.logspace(0, 1, 100) + .2 * 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']);

Controlling page layout - 图1

Using popouts to the right sidebar

Sometimes you want to quickly point out an extra piece of information, butyou don't want to break your narrative flow. Use a popout for this!

For example, here's some popout content! It was created by adding thepopout tag to a cell in the notebook. Jupyter Book automatically converts thesecells into helpful side content.

Add a popout cell by adding popout to your cell's tags. Here's what thecell metadata for a popout cell looks like:

  1. {
  2. "tags": [
  3. "popout",
  4. ]
  5. }

Popouts behave differently depending on the screen size. If the screen is narrowenough, the popout will exist in-line with your content. Make the screenwider and it'll pop out to the right.

Look to the right and play around with your window width to see how this looks!

You can include images in popouts! For example, let's see what the plot above looks like in a popout window.

Controlling page layout - 图2

My popout explanation. Since we've put a markdown cell underneath the code above,we can turn this into a popout as well, and use it as a quick caption to explainthe content above (we have hidden the code for the above plot with a remove_inputtag). However, in the case of this figure, there's not a whole lot to explain :-)

Quotations

There are two types of quotations in Jupyter Book: blockquotesand epigraphs.

Blockquotes are useful for calling out some information providedby another source. They don't break much with the content of thepage.

Here's an example of a blockquote. It is triggered by using the "caret" (>) symbol at the beginning of each line that you'd like to add a blockquote for. If you finish the quote with a - symbol, it will be treated as an "attribution" line and will be right-aligned.

  • Jo the Jovyan

Epigraphs are useful for introducing a particular page or idea.They're inspired by Edward Tufte's epigraph CSS.Epigraphs have a bit more emphasis and also differ in style a bit more fromyour page's content in order to draw attention to them.

Here's an example of an epigraph quote. Note that in this case, the quote itself is a bit larger and italicized. You probably shouldn't make this too long so that they don't stand out too much.

  • Jo the Jovyan

To indicate that you'd like an epigraph instead of a blockquote, addthe following tag your cell's tag list:

  1. {
  2. "tags": [
  3. "epigraph",
  4. ]
  5. }

Wide-format cells

Sometimes, you'd like to use all of the horizontal space available to you. This allowsyou to highlight particular ideas, visualizations, etc.

In Jupyter Book, you can specify that a cell (and its outputs) should take up all ofthe horizonal space (including the sidebar where popouts normally live) using thefollowing cell metadata tag:

  1. {
  2. "tags": [
  3. "full_width",
  4. ]
  5. }

For example, let's take a look at the figure at full-width. We'll tell Matplotlibto make it a bit wider so we can take advantage of the extra space!

  1. # Fixing random state for reproducibility
  2. np.random.seed(19680801)
  3.  
  4. N = 10
  5. data = [np.logspace(0, 1, 100) + .1*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=(20, 5))
  17. lines = ax.plot(data)
  18. ax.legend(custom_lines, ['Cold', 'Medium', 'Hot'])
  19. ax.set(title="Smoother linez");

Controlling page layout - 图3

Be careful about mixing popouts and full-width content. Sometimes these can conflictwith one another in visual space. You should use them relatively sparingly in orderfor them to have their full effect of highlighting information.

This page was created by The Jupyter Book Community