Fastpages Notebook Blog Post
A tutorial of fastpages for Jupyter notebooks.
¶
Front MatterThe first cell in your Jupyter Notebook or markdown blog post contains front matter. Front matter is metadata that can turn on/off options in your Notebook. It is formatted like this:
# "My Title"
> "Awesome summary"
- toc: true- branch: master- badges: true
- comments: true
- author: Hamel Husain & Jeremy Howard
- categories: [fastpages, jupyter]
- Setting
toc: true
will automatically generate a table of contents - Setting
badges: true
will automatically include GitHub and Google Colab links to your notebook. - Setting
comments: true
will enable commenting on your blog post, powered by utterances.
The title and description need to be enclosed in double quotes only if they include special characters such as a colon. More details and options for front matter can be viewed on the front matter section of the README.
¶
Markdown ShortcutsA #hide
comment at the top of any code cell will hide both the input and output of that cell in your blog post.
A #hide_input
comment at the top of any code cell will only hide the input of that cell.
put a #collapse-hide
flag at the top of any cell if you want to hide that cell by default, but give the reader the option to show it:
#collapse-hide
import pandas as pd
import altair as alt
put a #collapse-show
flag at the top of any cell if you want to show that cell by default, but give the reader the option to hide it:
#collapse-show
cars = 'https://vega.github.io/vega-datasets/data/cars.json'
movies = 'https://vega.github.io/vega-datasets/data/movies.json'
sp500 = 'https://vega.github.io/vega-datasets/data/sp500.csv'
stocks = 'https://vega.github.io/vega-datasets/data/stocks.csv'
flights = 'https://vega.github.io/vega-datasets/data/flights-5k.json'
place a #collapse-output
flag at the top of any cell if you want to put the output under a collapsable element that is closed by default, but give the reader the option to open it:
#collapse-output
print('The comment #collapse-output was used to collapse the output of this cell by default but you can expand it.')
¶
Interactive Charts With AltairCharts made with Altair remain interactive. Example charts taken from this repo, specifically this notebook.
¶
Example 1: DropDown# single-value selection over [Major_Genre, MPAA_Rating] pairs
# use specific hard-wired values as the initial selected values
selection = alt.selection_single(
name='Select',
fields=['Major_Genre', 'MPAA_Rating'],
init={'Major_Genre': 'Drama', 'MPAA_Rating': 'R'},
bind={'Major_Genre': alt.binding_select(options=genres), 'MPAA_Rating': alt.binding_radio(options=mpaa)}
)
# scatter plot, modify opacity based on selection
alt.Chart(df).mark_circle().add_selection(
selection
).encode(
x='Rotten_Tomatoes_Rating:Q',
y='IMDB_Rating:Q',
tooltip='Title:N',
opacity=alt.condition(selection, alt.value(0.75), alt.value(0.05))
)
¶
Example 2: Tooltipsalt.Chart(df).mark_circle().add_selection(
alt.selection_interval(bind='scales', encodings=['x'])
).encode(
alt.X('Rotten_Tomatoes_Rating', type='quantitative'),
alt.Y('IMDB_Rating', type='quantitative', axis=alt.Axis(minExtent=30)),
# y=alt.Y('IMDB_Rating:Q', ), # use min extent to stabilize axis title placement
tooltip=['Title:N', 'Release_Date:N', 'IMDB_Rating:Q', 'Rotten_Tomatoes_Rating:Q']
).properties(
width=500,
height=400
)
¶
Example 3: More Tooltips# select a point for which to provide details-on-demand
label = alt.selection_single(
encodings=['x'], # limit selection to x-axis value
on='mouseover', # select on mouseover events
nearest=True, # select data point nearest the cursor
empty='none' # empty selection includes no data points
)
# define our base line chart of stock prices
base = alt.Chart().mark_line().encode(
alt.X('date:T'),
alt.Y('price:Q', scale=alt.Scale(type='log')),
alt.Color('symbol:N')
)
alt.layer(
base, # base line chart
# add a rule mark to serve as a guide line
alt.Chart().mark_rule(color='#aaa').encode(
x='date:T'
).transform_filter(label),
# add circle marks for selected time points, hide unselected points
base.mark_circle().encode(
opacity=alt.condition(label, alt.value(1), alt.value(0))
).add_selection(label),
# add white stroked text to provide a legible background for labels
base.mark_text(align='left', dx=5, dy=-5, stroke='white', strokeWidth=2).encode(
text='price:Q'
).transform_filter(label),
# add text labels for stock prices
base.mark_text(align='left', dx=5, dy=-5).encode(
text='price:Q'
).transform_filter(label),
data=stocks
).properties(
width=500,
height=400
)
¶
Data TablesYou can display tables per the usual way in your blog:
# display table with pandas
df[['Title', 'Worldwide_Gross',
'Production_Budget', 'Distributor', 'MPAA_Rating', 'IMDB_Rating', 'Rotten_Tomatoes_Rating']].head()
¶
Remote ImagesRemote images can be included with the following markdown syntax:
![](https://image.flaticon.com/icons/svg/36/36686.svg)
¶
Animated GifsAnimated Gifs work, too!
![](https://upload.wikimedia.org/wikipedia/commons/7/71/ChessPawnSpecialMoves.gif)
¶
CaptionsYou can include captions with markdown images like this:
![](https://www.fast.ai/images/fastai_paper/show_batch.png "Credit: https://www.fast.ai/2020/02/13/fastai-A-Layered-API-for-Deep-Learning/")
¶
Other Elements¶
TweetcardsTyping > twitter: https://twitter.com/jakevdp/status/1204765621767901185?s=20
will render this:
Altair 4.0 is released! https://t.co/PCyrIOTcvv
— Jake VanderPlas (@jakevdp) December 11, 2019
Try it with:
pip install -U altair
The full list of changes is at https://t.co/roXmzcsT58 ...read on for some highlights. pic.twitter.com/vWJ0ZveKbZ
¶
Boxes / CalloutsTyping > Warning: There will be no second warning!
will render this:
Typing > Important: Pay attention! It's important.
will render this:
Typing > Tip: This is my tip.
will render this:
Typing > Note: Take note of this.
will render this:
Typing > Note: A doc link to [an example website: fast.ai](https://www.fast.ai/) should also work fine.
will render in the docs:
¶
FootnotesYou can have footnotes in notebooks, however the syntax is different compared to markdown documents. This guide provides more detail about this syntax, which looks like this:
For example, here is a footnote {% fn 1 %}.
And another {% fn 2 %}
{{ 'This is the footnote.' | fndetail: 1 }}
{{ 'This is the other footnote. You can even have a [link](www.github.com)!' | fndetail: 2 }}
For example, here is a footnote 1.
And another 2
1. This is the footnote.↩