thomasmock.quarto.pub/python-austin
Quarto is an open-source scientific and technical publishing system that builds on standard markdown with features essential for scientific communication.
Literate programming system in the tradition of Org-Mode, Weave.jl, R Markdown, iPyPublish, Jupyter Book, etc.
---
title: "matplotlib demo"
format:
html:
code-fold: true
jupyter: python3
---
For a demonstration of a line plot on a polar
axis, see @fig-polar.
```{python}
#| label: fig-polar
#| fig-cap: "A line plot on a polar axis"
import numpy as np
import matplotlib.pyplot as plt
r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r
fig, ax = plt.subplots(
subplot_kw = {'projection': 'polar'}
)
ax.plot(theta, r)
ax.set_rticks([0.5, 1, 1.5, 2])
ax.grid(True)
plt.show()
```
Can be rendered to dozens of output formats with Quarto (via Pandoc):
Feature | Quarto |
---|---|
Basic Formats | |
Beamer | |
PowerPoint | |
HTML Slides | |
Advanced Layout |
Feature | Quarto |
---|---|
Cross References | |
Websites & Blogs | |
Books | |
Interactivity | |
Dashboards | Quarto Dashboards |
Journal Articles | Out and more coming! |
Quarto® is an open-source scientific and technical publishing system built on Pandoc.
Rendering (execute and write to disk):
For execution of R, Quarto uses knitr
as the engine, but for Python Quarto natively executes Python with Jupyter kernels such as IPython.
{python}
executable cells are present. You can set a specific kernel via the YAML:IPython executes Python code and transforms it to plain text, graphics, markdown, HTML, etc.
For interactive sessions, Quarto keeps the Jupyter Kernel resident as a daemon to mitigate startup times.
.qmd
is a plain text fileCode
Plain text workflow (.qmd
uses Jupyter kernel to execute cells):
Notebook workflow (defaults to using existing stored computation):
.ipynb
?You can keep using them! You get to choose whether to use the stored computation OR re-execute the document from top to bottom.
Quarto can help convert back and forth between plain text .qmd
and .ipynb
- kind of like jupytext
but specific to Quarto:
quarto convert --help
Usage: quarto convert <input>
Description:
Convert documents to alternate representations.
Convert notebook to markdown: quarto convert doc.ipynb
Convert markdown to notebook: quarto convert doc.qmd
Convert notebook to markdown, write to file: quarto convert doc.ipynb --output doc.qmd
.ipynb
which is JSON).qmd
and/or .ipynb
).html
or .pdf
).json
, allows for permanately saving and re-use of computational outputs across entire project.Both RStudio and VSCode with the Quarto extension have rich auto-completion
Add two images on disk to a two column layout.
Two plots from code, layout in two columns.
```{python}
#| layout-ncol: 2
#| fig-cap: ["Scatter", "Boxplot"]
from plotnine import ggplot, geom_point, geom_boxplot, aes, stat_smooth, facet_wrap, theme
from plotnine.data import mtcars
# plot 1 in column 1
plot1 = (ggplot(mtcars, aes('wt', 'mpg', color='factor(gear)'))
+ geom_point() + stat_smooth(method='lm')
+ facet_wrap('~gear')).draw(show=True)
# plot 2 in column 2
plot2 = (ggplot(mtcars, aes('cyl', 'mpg', color='factor(cyl)'))
+ geom_boxplot()).draw(show=True)
```
Live example at Quarto Manuscripts
---
title: "Development Indicators by Continent"
author: "Gapminder Analytics Group"
format: dashboard
---
```{python}
import plotly.express as px
df = px.data.gapminder()
```
## Row {height=60%}
```{python}
#| title: GDP and Life Expectancy
px.scatter(
df, x="gdpPercap", y="lifeExp",
animation_frame="year", animation_group="country",
size="pop", color="continent", hover_name="country",
facet_col="continent", log_x=True, size_max=45,
range_x=[100,100000], range_y=[25,90]
)
```
## Row {height=40%}
```{python}
#| title: Population
px.area(
df, x="year", y="pop",
color="continent", line_group="country"
)
```
```{python}
#| title: Life Expectancy
px.line(
df, x="year", y="lifeExp",
color="continent", line_group="country"
)
```
title
and author
for the navigation bar as well as specifying the use of the dashboard
format.
height
option.
title
option).
Quarto also includes native support for Observable JS, a set of enhancements to vanilla JavaScript created by Mike Bostock (also the author of D3)
Quarto including Observable means you can create new “widgets” or allow the user to modify portions of the doc on the fly.
Converting temperature from ℃ to ℉
Celsius = ℃ and Fahrenheit = ℉.
#| standalone: true
#| viewerHeight: 420
#| echo: false
#| eval: true
from shiny import App, render, ui
import numpy as np
import matplotlib.pyplot as plt
app_ui = ui.page_fluid(
ui.layout_sidebar(
ui.panel_sidebar(
ui.input_slider("period", "Period", 0.5, 4, 1, step=0.5),
ui.input_slider("amplitude", "Amplitude", 0, 2, 1, step=0.25),
ui.input_slider("shift", "Phase shift", 0, 2, 0, step=0.1),
),
ui.panel_main(
ui.output_plot("plot"),
),
),
)
def server(input, output, session):
@output
@render.plot(alt="Sine wave")
def plot():
t = np.arange(0.0, 4.0, 0.01)
s = input.amplitude() * np.sin(
2 * np.pi / input.period() * (t - input.shift() / 2)
)
fig, ax = plt.subplots()
ax.set_ylim([-2, 2])
ax.plot(t, s)
ax.grid()
app = App(app_ui, server)
No server needed, all in browser!
```{shinylive-python}
#| standalone: true
#| viewerHeight: 420
from shiny import App, render, ui
import numpy as np
import matplotlib.pyplot as plt
app_ui = ui.page_fluid(
ui.layout_sidebar(
ui.panel_sidebar(
ui.input_slider("period", "Period", 0.5, 4, 1, step=0.5),
ui.input_slider("amplitude", "Amplitude", 0, 2, 1, step=0.25),
ui.input_slider("shift", "Phase shift", 0, 2, 0, step=0.1),
),
ui.panel_main(
ui.output_plot("plot"),
),
),
)
def server(input, output, session):
@output
@render.plot(alt="Sine wave")
def plot():
t = np.arange(0.0, 4.0, 0.01)
s = input.amplitude() * np.sin(
2 * np.pi / input.period() * (t - input.shift() / 2)
)
fig, ax = plt.subplots()
ax.set_ylim([-2, 2])
ax.plot(t, s)
ax.grid()
app = App(app_ui, server)
```
To render using different parameters you can pass them on the command line using the -P
flag:
One goal of Quarto is to provide a markdown-centric format-agnostic syntax as shown in previous slides.
Development of Quarto is sponsored by Posit, PBC (formerly known as RStudio, PBC). The same core team works on both Quarto and R Markdown:
Carlos Scheidegger (@cscheid)
Charles Teague (@dragonstyle)
Christophe Dervieux (@cderv)
J.J. Allaire (@jjallaire)
Here is the full contributors list. Quarto is open source and we welcome contributions in our github repository as well! https://github.com/quarto-dev/quarto-cli.
Follow @quarto_pub #QuartoPub on Twitter/Fosstodon to stay up to date!