05:00
BAA1028 - Workflow & Data Management
Start again from scratch!
You have made incredible progress with your eportfolio but you should not mess it up. Let’s practice on a fresh website!
In the terminal of VS Code type:
Today, we will learn how to add code to project pages.
Project Pages on your website will serve as dedicated sections showcasing specific projects or topics.
Some pages will focus on academic or personal, non-coding projects, such as group work or volunteer experiences, using only Markdown and illustrative content.
Others will feature coding projects, incorporating both Markdown and code snippets within your Quarto pages.
Today’s lecture will concentrate on integrating code into your Project Pages effectively.
Use ```
to delimit blocks of source code:
Add a language to syntax highlight code blocks
Markdown supports syntax highlighting for over 140 different languages.
If your language is not supported then you can use the default
language to get a similar visual treatment:
Equivalent to the short form used in the examples above is a longer form that uses the language as a class (i.e. .python
) inside braces
The longer form allows you to add attributes to the block in a similar way to Divs. Some specific features that use this syntax are Lines Numbers and Code Filename.
Here is an example of the latter:
If you are creating HTML output there is a wide variety of options available for code block output. See the article on HTML Code for additional details.
Write a project page which only includes non active code to describe how to read a csv file, select some columns, and filter some values with the pandas
library in python.
.qmd
document in your new websitepandas
library in python.qmd
document to display non-active python code05:00
Instead of just showing the code in a fancy way, Quarto can also run the code from the code chunks and print out their outputs.
In stand alone Quarto document you would specify the engine: python3
argument in your YAML but in a website, there is no action to take beside including “Active” code cells/chunks.
An “Active” python code cells/chunks uses ```{python}
to start the cells/chunks and ```
to finish it.
What you write:
What you see:
Place options underneath, behind the #|
(hashpipe): #| option1: value
Cells output can be customized with this hashpipe options (i.e., arguments set after the #|
). Above, we use 1 argument:
See the Quarto Reference Guide for a complete list of chunk options.
As long as an object is created in the first chunk, then a visualisation can be used in a separated cell:
Some cell options are specific to visualisations outputs:
Note, the default unit for height and width is inches.
In your tutorial to use the pandas
library in python, turn all the non active code cells/chunks into active code.
05:00
Write a second project page which describes how to do a linear regression in python.
This project page should include some python visualisations.
Add hashpipe options to change the output of these visualisations.
05:00
Some options specified for each code cell/chunk with a hashpipe can also been set globally for the whole document in the YAML
Instead of:
The option code-line-numbers:true
or code-line-numbers:false
includes or hiddes line numbers in code block output
Instead of:
For example, the option code-fold: true
collapses the code into an HTML “details” tag so the user can display it on-demand.
What you write:
You can include a Code menu in the header of your document that provides various tools for readers to interact with the source code. Specify code-tools: true
to activate these tools:
If you have a document that includes folded code blocks then the Code menu will present options to show and hide the folded code as well as view the full source code of the document:
This document specifies code-tools: true
in its options so you should see the Code menu above next to the main header.
You can control which of these options are made available as well as the “Code” caption text using sub-options of code-tools
.
For example, here we specify that we want only “View Source” (no toggling of code visibility) and no caption on the code menu:
Note that the code-tools
option is not available when you disable the standard HTML theme (e.g. if you specify the theme: none
option).
By default code blocks are rendered with a left border whose color is derived from the current theme.
You can customize code chunk appearance with some simple options that control the background color and left border. Options can either be booleans to enable or disable the treatment or can be legal CSS color strings (or they could even be SASS variable names!).
Here is the default appearance for code blocks (code-block-background: true
):
You can combine a background and border treatment as well as customize the left border color:
Quarto will automatically highlight syntax in fenced code blocks that are marked with a language name.
For example:
You can specify the code highlighting style using highlight-style
and specifying one of the supported themes. These themes are “adaptive”, which means they will automatically switch between a dark and light mode based upon the theme of the website.
These are designed to work well with sites that include a dark and light mode.
All of the standard Quarto themes are also available:
As well as an additional set of extended themes, including:
The highlight-style
option determines which theme is used.
For example:
Highlighting themes can provide either a single highlighting definition or two definitions, one optimized for a light colored background and another optimized for a dark color background.
When available, Quarto will automatically select the appropriate style based upon the code chunk background color’s darkness.
By default, code is highlighted using the arrow
theme, which is optimized for accessibility.
Examples of the light and dark themes:
Thanks for your attention and don’t hesitate to ask if you have any questions!
@damien_dupre
@damien-dupre
https://damien-dupre.github.io
damien.dupre@dcu.ie