Practical example
Introduction
You will learn how to create your first static website with Quarto.
1 GitHub repository
To publish our website, we will use GitHub Pages. The first step is to create a repository on GitHub.
-
- Visibility : Public
- check Add a README file
- .gitignore : GitHubPages
- licence : Creative Commons Zero v1.0 Universal
-
- clic on button
- write gh-pages
- clic on
Create branch gh-pages from main
- wait 30 seconds and then refresh the page until a Deployments section appears at the bottom right
This will keep your branch main
clean and avoid including html pages generated by Quarto :
- branch
main
: setup and content in qmd files - branch
gh-pages
: html for deployment
-
- Check Use your GitHub Pages website
You can clic on the url. Your website is created but empty.
2 Local repository
We will use the IDE Visual Studio Code to edit our pages.
-
- terminal > New terminal or CTRL + ù or CTRL + SHIFT + C
-
- File > Open Folder > /home/onyxia/work/Quarto-practical
The license chosen in GitHub is too permissive. Let’s change it for a more restrictive one
-
- BY (Attribution): Credit must be given to the creator
- NC (Non Commercial): Only noncommercial uses of the work are permitted
- SA (Share Alike): Adaptations must be shared under the same terms
3 Create a Quarto project
Let’s start by creating a quarto project. We’re going to create a website project, i.e. a collection of web pages.
-
- Type: website
- Directory: .
- Title: My first quarto site
This command will create the following files in the current directory (.
):
- _quarto.yml: metadata
- index.qmd: the home page
- about.qmd: another page (not a mandatory file)
- styles.css: to customize visual aspects of your Quarto website
It is also possible to create these files manually.
4 Preview your site
You can open the preview on a specific port.
CTRL + C to quit the preview
5 Edit your files
Now it’s time to custom our website.
5.1 index.qmd
Let’s modify the index:
- adding some local metadata (between
---
) - inserting link to other pages
- putting an emoji
---
title: "Quarto-practical"
description: "Let's have fun with Quarto"
author: "Me"
format: html
from: markdown+emoji
---
## Quarto
- [External link to Quarto website](https://quarto.org/){target="_blank"}
- [R example](doc/r.qmd) - [Python example](doc/python.qmd) :snake:
You will see this in the terminal. This is normal, as we haven’t yet created these files.
WARN: Unable to resolve link target: doc/r.qmd
WARN: Unable to resolve link target: doc/python.qmd
5.2 _quarto.yml
This configuration file holds metadata settings.
All files within your repository will inherit these metadata settings. However, you have the flexibility to redefine these values for each individual file.
-
project: type: website website: title: "Quarto tutorial" navbar: background: lightseagreen left: - href: index.qmd text: Home - text: Examples with code menu: - href: doc/r.qmd text: "R code" - href: doc/python.qmd text: "Python code" right: - about.qmd format: html: theme: dark: solar light: cosmo css: styles.css
- project type :
website
, let’s say it’s the default value - there is a navigation bar with a single tab
- tab name : Home
- it links to the
index.qmd
file ➡️ we will create it in the root
- the default format is
html
- with two themes : one for the dark mode (first one so it is the default theme) and one for light mode
- on the navbar top left you will find a trigger to toggle theme from dark to light
- it use styles from
styles.css
➡️ we will create it in the root
- with two themes : one for the dark mode (first one so it is the default theme) and one for light mode
5.3 styles.css
In this file you can define all styles you can imagin.
-
.green-button { display: inline-block; padding: 2px 5px; font-size: 14px; color: white; background-color: green; border: none; border-radius: 12px; text-align: center; text-decoration: none; cursor: pointer; transition: background-color 0.3s ease; } .green-button:hover { background-color: darkgreen; }
-
[a button]{.green-button}
5.4 Create new files
First of all, we will create a folder to store our files.
It’s important to work cleanly and store your files properly.
5.4.1 Using R Code
r.qmd
---
title: "R code"
author: "Me"
format: html
from: markdown+emoji
code-block-bg: true
code-block-border-left: "#31BAE9"
---
```{R}
<- 0.5
alpha <- 0.5
beta <- 500
n_values
set.seed(123)
<- rbeta(n = n_values, shape1 = alpha, shape2 = beta)
beta_values
plot(density(beta_values),
main = expression(paste("Density of Beta(", alpha, " = 0.5, ", beta, " = 0.5) Distribution")),
xlab = "Value",
ylab = "Density",
col = "blue",
lwd = 2,
cex.main = 1.2,
cex.lab = 1.1,
cex.axis = 1.0
)```
5.4.2 Using Python Code
To run python cells, we have to install jupyter
and needed packages
python.qmd
---
title: "Python code"
author: "Me"
format: html
from: markdown+emoji
code-block-bg: true
code-block-border-left: orange
---
```{python}
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
= np.linspace(-5, 5, 1000)
x
= norm.pdf(x, 0, 1)
pdf ='Normal Distribution')
plt.plot(x, pdf, label
plt.show()```
6 Publish
We will use git to synchronise our repositories and deploy our pages on GitHub.
6.1 .gitignore
You may have noticed that a _site folder has been created (during the preview). This folder contains the html pages generated from the qmd files. It should not be versioned with git.
6.2 .github/workflows/publish.yml
You’ve just created a Quarto website on your computer.
Now, you want to publish it online using GitHub Pages.
To do that, imagine GitHub gives you a brand new, empty computer every time you update your site. On this empty machine, you need to:
- Download your project files
- Install the needed tools (Quarto, R, Python, etc.)
- Install the packages your site uses
- Build (render) the website
- Publish it to GitHub Pages
All these steps are written in a script (called a GitHub Actions workflow), so GitHub can do them automatically every time you update your site.
.github/workflows/publish.yml
on:
workflow_dispatch:
push:
branches: main
name: Quarto Publish
jobs:
build-deploy:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Check out repository
uses: actions/checkout@v4
- name: Set up Quarto
uses: quarto-dev/quarto-actions/setup@v2
- name: Install R
uses: r-lib/actions/setup-r@v2
with:
r-version: '4.3.0'
- name: Install R packages from DESCRIPTION file
uses: r-lib/actions/setup-r-dependencies@v2
- name: Install Python and Dependencies
uses: actions/setup-python@v4
with:
python-version: '3.10'
cache: 'pip'
- run: pip install jupyter
- run: pip install -r requirements.txt
- name: Render and Publish
uses: quarto-dev/quarto-actions/publish@v2
with:
target: gh-pages
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6.3 List R and Python Packages
As you probably know, R and Python have many packages.
DESCRIPTION
Package: quartotuto
Type: website
Title: Quarto tuto
Description: Used to declare R dependencies
Version: 1.0
Authors@R: c(
person("Ludovic", "DENEUVILLE",role = c("aut", "cre"), email = "ludovic.deneuville@ensai.fr"))
URL: https://ludo2ne.github.io/Quarto-tuto
License: MIT
Imports:
dplyr,
ggplot2,
leaflet,
rmarkdown Encoding: UTF-8
requirements.txt
matplotlib
numpy scipy
6.4 git push
7 Gear second
Let’s go further.