Kernels

Most of the time, Jupyter Lab will be used with the seminar or seminar-r kernel. By default, the seminar kernel runs Python code and seminar-r kernel runs R code. To run other types of code, see below. Any format or template related questions should be asked in Piazza.

Running Python code using the seminar kernel

import pandas as pd
myDF = pd.read_csv("/anvil/projects/tdm/data/flights/subset/airports.csv")
myDF.head()

Running R code using the seminar kernel or the seminar-r kernel

Using the seminar kernel with R, it is necessary to use the %%R cell magic:

%%R

myDF <- read.csv("/anvil/projects/tdm/data/flights/subset/airports.csv")
head(myDF)

Using the seminar-r kernel with R, it is NOT necessary to use the %%R cell magic:

myDF <- read.csv("/anvil/projects/tdm/data/flights/subset/airports.csv")
head(myDF)

As you can see, any cell that begins with %%R and uses the seminar kernel will run the R code in that cell. Alternatively, using the seminar-r kernel, it is possible to run R code without using the %%R cell magic.

Running SQL queries using the seminar kernel

  1. First, you need to establish a connection with the database. If this is a sqlite database, you can use the following command.

    %sql sqlite:///my_db.db
    # or
    %sql sqlite:////absolute/path/to/my_db.db
    # like this
    %sql sqlite:////anvil/projects/tdm/data/movies_and_tv/imdb.db

    Otherwise, if this is a mysql database, you can use the following command.

    %sql mariadb+pymysql://username:password@my_url.com/my_database
  2. Next, we can run SQL queries, in a new cell, as shown with the following example, in which we show the first 5 lines of the titles table.

    %%sql
    
    SELECT * FROM titles LIMIT 5;

As you can see, any cell that begins with %%sql will run the SQL query in that cell. If a cell does not begin with %%sql, it will be assumed that the code is Python code, and run accordingly.

Running bash code using the seminar kernel

To run bash code, in a new cell, run the following.

%%bash

head /anvil/projects/tdm/data/flights/subset/airports.csv

As you can see, any cell that begins with %%bash will run the bash code in that cell. If a cell does not begin with %%bash, it will be assumed that the code is Python code, and run accordingly.

Code cells that start with % or %% are sometimes referred to as magic cells. To see a list of available magics, run %lsmagic in a cell.

The commands listed in the "cell" section are run with a double %% and apply to the entire cell, rather than just a single line. For example, %%bash is an example of a cell magic.

You can read more about some of the available magics in the official documentation.

Including an image in your notebook

To include an image in your notebook, use the following Python code.

from IPython import display
display.Image("/anvil/projects/tdm/data/images/woodstock.png")

Here, /anvil/projects/tdm/data/images/woodstock.png is the path to the image you would like to include.

If you choose to include an image using a Markdown cell, and the ![](…​) syntax, please note that while the notebook will render properly in our ondemand.anvil.rcac.purdue.edu environment, it will not load properly in any other environment where that image is not available. For this reason it is critical to include images using the method shown here.