Skip to content

Commit 7fddc61

Browse files
cgraham-rsdotNomad
andauthored
Add quarto-script-python (#96)
* Migrate jump start quarto-script-python * Do not .gitignore manifest.json * Update min Quarto version in manifest * Adjust min Python version * Adjust min Python version in manifest * Add tags, quarto requires * Update manifest to remove .Rprofile * Use example category * Rename as script-python and refactor some of the gallery language * Fix homepage link --------- Co-authored-by: Jordan Jensen <[email protected]>
1 parent b086f7e commit 7fddc61

File tree

8 files changed

+343
-0
lines changed

8 files changed

+343
-0
lines changed

.github/workflows/extensions.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ jobs:
4444
quarto-stock-report-r: extensions/quarto-stock-report-r/**
4545
stock-api-fastapi: extensions/stock-api-fastapi/**
4646
quarto-presentation: extensions/quarto-presentation/**
47+
script-python: extensions/script-python/**
4748
connectwidgets-example: extensions/connectwidgets-example/**
4849
plumbertableau-example: extensions/plumbertableau-example/**
4950
fastapitableau-example: extensions/fastapitableau-example/**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/.quarto/
2+
/env/
3+
*.csv
4+
*.json
5+
script.html
6+
script_files
7+
/email-preview/
8+
.output_metadata.json
9+
10+
# But do not ignore manifest.json
11+
!manifest.json

extensions/script-python/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Run a Python Script using Quarto
2+
3+
## About this example
4+
5+
This data transformation script uses Quarto and Python. It is an example
6+
of how you might automate regular imports and transformations from a data
7+
source. JSON and CSV files are exported and made available for download.
8+
9+
The script also generates a custom email, with the generated CSV files as
10+
attachments.
11+
12+
13+
## Learn more
14+
15+
* [Quarto](https://quarto.org)
16+
* [Quarto: Rendering Script Files](https://quarto.org/docs/computations/render-scripts.html)
17+
18+
## Requirements
19+
20+
* Quarto version 1.4 or higher
21+
* Python version 3.8 or higher
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
project:
2+
title: "Penguin data transformations"
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"version": 1,
3+
"locale": "en_US.UTF-8",
4+
"metadata": {
5+
"appmode": "quarto-static"
6+
},
7+
"extension": {
8+
"name": "script-python",
9+
"title": "Run a Python Script",
10+
"description": "This data transformation Python script is an example of how you might automate regular imports and transformations from a data source. JSON and CSV files are exported and made available for download. Quarto is used to render the script.",
11+
"homepage": "https://github.com/posit-dev/connect-extensions/tree/main/extensions/script-python",
12+
"category": "example",
13+
"minimumConnectVersion": "2025.04.0",
14+
"version": "1.0.0"
15+
},
16+
"environment": {
17+
"python": {
18+
"requires": "~=3.8"
19+
},
20+
"quarto": {
21+
"requires": "~=1.4"
22+
}
23+
},
24+
"quarto": {
25+
"version": "1.4.557",
26+
"engines": [
27+
"jupyter"
28+
]
29+
},
30+
"python": {
31+
"version": "3.8.20",
32+
"package_manager": {
33+
"name": "pip",
34+
"version": "23.0.1",
35+
"package_file": "requirements.txt"
36+
}
37+
},
38+
"files": {
39+
"requirements.txt": {
40+
"checksum": "5a2845f6ec11bcefce420b49ef94c152"
41+
},
42+
".gitignore": {
43+
"checksum": "42b3cf8550e12acf456cef1254151634"
44+
},
45+
"README.md": {
46+
"checksum": "87c06eb724cdf54ed602741c3a82cc71"
47+
},
48+
"_quarto.yml": {
49+
"checksum": "e5bfdf389c2ae3bc115d8f4add41fc2f"
50+
},
51+
"script.py": {
52+
"checksum": "46d490184f5d440c884c6fd28460b54f"
53+
},
54+
"script.quarto_ipynb": {
55+
"checksum": "e032e1bd96459a32494df48ea2e58194"
56+
}
57+
}
58+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# jupyter
2+
notebook==7.2.2
3+
qtconsole==5.5.1
4+
jupyter-console==6.6.3
5+
nbconvert==7.16.1
6+
ipykernel==6.29.3
7+
ipywidgets==8.1.2
8+
9+
palmerpenguins==0.1.4

extensions/script-python/script.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# %% [markdown]
2+
# ---
3+
# title: "Penguin data transformations"
4+
# subtitle: "Exported as JSON and CSV"
5+
# format: email
6+
# email-attachments:
7+
# - "adelie-m.csv"
8+
# - "gentoo-f.csv"
9+
# ---
10+
11+
# %% [markdown]
12+
# ## Setup
13+
# %%
14+
import palmerpenguins
15+
16+
penguins = palmerpenguins.load_penguins()
17+
18+
# %% [markdown]
19+
# ## Filtering
20+
# %%
21+
gentoo_f = penguins[
22+
(penguins["species"] == "Gentoo") &
23+
(penguins["sex"] == "female")
24+
]
25+
adelie_m = penguins[
26+
(penguins["species"] == "Adelie") &
27+
(penguins["sex"] == "male")
28+
]
29+
30+
# %% [markdown]
31+
# ## Statistics (Gentoo)
32+
# %%
33+
gentoo_f.describe()
34+
35+
# %% [markdown]
36+
# ## Statistics (Adelie)
37+
# %%
38+
adelie_m.describe()
39+
40+
41+
# %% [markdown]
42+
# ## Export
43+
# %%
44+
gentoo_f.to_csv("gentoo-f.csv")
45+
gentoo_f.to_json("gentoo-f.json", orient="records")
46+
adelie_m.to_csv("adelie-m.csv")
47+
adelie_m.to_json("adelie-m.json", orient="records")
48+
49+
# %% [markdown]
50+
# # Exported data
51+
#
52+
# * [gentoo-f.json](gentoo-f.json)
53+
# * [gentoo-f.csv](gentoo-f.csv)
54+
# * [adelie-m.json](adelie-m.json)
55+
# * [adelie-m.csv](adelie-m.csv)
56+
57+
# %% [markdown]
58+
#
59+
# ::: {.email}
60+
# ::: {.subject}
61+
# Penguin data files
62+
# :::
63+
64+
# %%
65+
#| echo: false
66+
#| output: asis
67+
print("Identified", len(gentoo_f), "female Gentoo penguins.")
68+
print("Identified", len(adelie_m), "male Adelie penguins.")
69+
print("")
70+
print("CSV files are attached.")
71+
72+
# %% [markdown]
73+
# :::
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"---\n",
8+
"title: \"Penguin data transformations\"\n",
9+
"subtitle: \"Exported as JSON and CSV\"\n",
10+
"format: email\n",
11+
"email-attachments:\n",
12+
" - \"adelie-m.csv\"\n",
13+
" - \"gentoo-f.csv\"\n",
14+
"---\n",
15+
"\n",
16+
"\n",
17+
"## Setup\n"
18+
],
19+
"id": "05b2b162"
20+
},
21+
{
22+
"cell_type": "code",
23+
"metadata": {},
24+
"source": [
25+
"import palmerpenguins\n",
26+
"\n",
27+
"penguins = palmerpenguins.load_penguins()"
28+
],
29+
"id": "d0398bb8",
30+
"execution_count": null,
31+
"outputs": []
32+
},
33+
{
34+
"cell_type": "markdown",
35+
"metadata": {},
36+
"source": [
37+
"## Filtering\n"
38+
],
39+
"id": "d9632eb2"
40+
},
41+
{
42+
"cell_type": "code",
43+
"metadata": {},
44+
"source": [
45+
"gentoo_f = penguins[\n",
46+
" (penguins[\"species\"] == \"Gentoo\") &\n",
47+
" (penguins[\"sex\"] == \"female\")\n",
48+
"]\n",
49+
"adelie_m = penguins[\n",
50+
" (penguins[\"species\"] == \"Adelie\") &\n",
51+
" (penguins[\"sex\"] == \"male\")\n",
52+
"]"
53+
],
54+
"id": "180dd4e3",
55+
"execution_count": null,
56+
"outputs": []
57+
},
58+
{
59+
"cell_type": "markdown",
60+
"metadata": {},
61+
"source": [
62+
"## Statistics (Gentoo)\n"
63+
],
64+
"id": "2c56f743"
65+
},
66+
{
67+
"cell_type": "code",
68+
"metadata": {},
69+
"source": [
70+
"gentoo_f.describe()"
71+
],
72+
"id": "19650928",
73+
"execution_count": null,
74+
"outputs": []
75+
},
76+
{
77+
"cell_type": "markdown",
78+
"metadata": {},
79+
"source": [
80+
"## Statistics (Adelie)\n"
81+
],
82+
"id": "f957bb3e"
83+
},
84+
{
85+
"cell_type": "code",
86+
"metadata": {},
87+
"source": [
88+
"adelie_m.describe()"
89+
],
90+
"id": "9ee32e66",
91+
"execution_count": null,
92+
"outputs": []
93+
},
94+
{
95+
"cell_type": "markdown",
96+
"metadata": {},
97+
"source": [
98+
"## Export\n"
99+
],
100+
"id": "b71c1828"
101+
},
102+
{
103+
"cell_type": "code",
104+
"metadata": {},
105+
"source": [
106+
"gentoo_f.to_csv(\"gentoo-f.csv\")\n",
107+
"gentoo_f.to_json(\"gentoo-f.json\", orient=\"records\")\n",
108+
"adelie_m.to_csv(\"adelie-m.csv\")\n",
109+
"adelie_m.to_json(\"adelie-m.json\", orient=\"records\")"
110+
],
111+
"id": "7edff85c",
112+
"execution_count": null,
113+
"outputs": []
114+
},
115+
{
116+
"cell_type": "markdown",
117+
"metadata": {},
118+
"source": [
119+
"# Exported data\n",
120+
"\n",
121+
"* [gentoo-f.json](gentoo-f.json)\n",
122+
"* [gentoo-f.csv](gentoo-f.csv)\n",
123+
"* [adelie-m.json](adelie-m.json)\n",
124+
"* [adelie-m.csv](adelie-m.csv)\n",
125+
"\n",
126+
"\n",
127+
"::: {.email}\n",
128+
"::: {.subject}\n",
129+
"Penguin data files\n",
130+
":::\n"
131+
],
132+
"id": "89f8e637"
133+
},
134+
{
135+
"cell_type": "code",
136+
"metadata": {},
137+
"source": [
138+
"#| echo: false\n",
139+
"#| output: asis\n",
140+
"print(\"Identified\", len(gentoo_f), \"female Gentoo penguins.\")\n",
141+
"print(\"Identified\", len(adelie_m), \"male Adelie penguins.\")\n",
142+
"print(\"\")\n",
143+
"print(\"CSV files are attached.\")"
144+
],
145+
"id": "eed89b62",
146+
"execution_count": null,
147+
"outputs": []
148+
},
149+
{
150+
"cell_type": "markdown",
151+
"metadata": {},
152+
"source": [
153+
":::\n"
154+
],
155+
"id": "c6118388"
156+
}
157+
],
158+
"metadata": {
159+
"kernelspec": {
160+
"name": "python3",
161+
"language": "python",
162+
"display_name": "Python 3 (ipykernel)",
163+
"path": "/Users/christophergraham/.pyenv/versions/3.11.7/share/jupyter/kernels/python3"
164+
}
165+
},
166+
"nbformat": 4,
167+
"nbformat_minor": 5
168+
}

0 commit comments

Comments
 (0)