Lecture 13A: From Notebooks to the Web: Github Pages, Web Servers, and Dash

November 24, 2020

Housekeeping

Final project due: 5pm on Monday, December 21st

Outline for the rest of the course

We'll discuss ways to translate our analysis results to the Web in a meaningful way. We'll cover three methods to do so, each with their own pros and cons:

  1. Embedding interactive charts on the Web via Github Pages
  2. Creating Web apps & dashboards using the Python library Dash
  3. Creating Web apps & dashboards using the Python library Panel

Today: we'll focus on the first two today and cover Panel in detail in week 14

Part 1: Embedding Interactive Charts on the Web

To start, let's recap three ways that we've learned to produce interactive charts in the course:

Example 1: Measles Incidence in Altair

Load the data from week 2:

Use the pandas.melt() function to convert it to tidy format:

Now let's load altair:

Saving Altair Plots

Altair plots can be fully represented as JSON data. This makes them very easy to embed on websites, as we shall soon see!

Now, let's compare the HTML and JSON files...

Measles Incidence in hvplot

Saving Hvplot Plots

HTML is are only option here...

Example 3: Folium + OSMnx

Identify the lat/lng coordinates for our places of interest: Use osmnx to download the geometries for the Libery Bell and Art Museum

Get the street graph in Center City: Use osmnx to download the street network around City Hall.

Identify the nodes in the graph closest to our points of interest.

Use networkx to find the shortest path

Saving Folium Maps

Just use the save() function!

How can we display these charts on the Web?

We can embed them on GitHub Pages...

Part 2: Github Pages

Template Site with Examples

Github Pages URL

The structure of the URL for the rendered page is:

https://[USERNAME].github.io/[REPOSITORY NAME]

Note: you can create a new website for every repository, so this will work even if you have a personal Github Pages website set up.

Step 1: Create your own repository


For more information, see this guide on creating a repository from a template.

Step 2: Choose a name and description

Step 3: Enable Github Pages on your new repository

On the home page for your new repository go to "Settings":

Step 4: Customize your site

For more information on the config file, see the documentation.

Step 5: Publish a post

Adding new posts

To add new posts, simply add a file in the _posts directory that:

  1. follows the convention YYYY-MM-DD-name-of-post.ext
  2. Includes the necessary header material (see the next slide)

You can take a look at the source for the existing posts in the _posts folder to get an idea about how it works.

The anatomy of a post

Source: https://raw.githubusercontent.com/MUSA-550-Fall-2020/github-pages-starter/master/_posts/2019-04-17-example-post.md

Rendered: https://musa-550-fall-2020.github.io/github-pages-starter/example-post/

How can we embed our (static) Matplotlib charts?

These are just normal PNG images — we can use Markdown's syntax for embedding images.

Embedding images: the syntax

![alt-text]({{ site.url }}{{ site.baseurl }}/assets/images/YOUR_IMAGE_FILE.png

Steps

  1. Place your image in the assets/images/ folder
  2. Change YOUR_IMAGE_FILE.png to the name of your image and leave the rest of the path unchanged.

Note: the curly brackets for site.url and site.baseurl are template variables. When the site is rendered, these variables automatically get filled in so that the absolute path to the PNG file is correct.

How can we embed our interactive charts?

In the header, we can specify the charts to load using special loaders, which can handle three different types of files:

Embedding Altair/Hvplot charts: example

Source: https://raw.githubusercontent.com/MUSA-550-Fall-2020/github-pages-starter/master/_posts/2019-04-13-measles-charts.md

Rendered: https://musa-550-fall-2020.github.io/github-pages-starter/measles-charts/

Embedding Altair/Hvplot charts: the header

Header syntax

Under the altair-loader and hv-loader we need to specify two things:

  1. The CSS "id" attribute for the div where we want the chart to go
  2. The path to the HTML or JSON file to load (must be in the charts/ folder)

This should be specified as key: value pairs, for example:

altair-chart-1: "charts/measlesAltair.json"

In this case altair-chart-1 is the CSS identifier and "charts/measlesAltair.json" is the name of the file to load.

Note: there must be a matching "div" element with the same CSS identifier — this is where the chart will be embedded!

Important

Embedding Altair/Hvplot charts: the content

Embedding Folium charts: example

Source: https://raw.githubusercontent.com/MUSA-550-Fall-2020/github-pages-starter/master/_posts/2019-04-13-folium-charts.md

Rendered: https://musa-550-fall-2020.github.io/github-pages-starter/folium-charts/

Embedding Folium charts: the header

Header syntax

For the folium-loader, the syntax is:

CSS identifier for chart : ["chart file path", "width of chart in pixels"]

Again, there must be a matching "div" element with the same CSS identifier — this is where the chart will be embedded!

Embedding Folium charts: the content

So, which files are important?

There's a lot of extra stuff we don't actually need. The most important files/folders are:

Exercise: Setup your own Github Pages project page

Steps:

Notes

Remember: we can save altair charts as JSON files and use the altair-loader to directly load the JSON representation.