Python Interactive Environment Setup
Quarto Live Installation
Install the Quarto Live extension:
quarto add r-wasm/quarto-live
Configure YAML header for interactive Python:
---
format: live-html
---
Detailed guide: Installing Quarto Live & WebAssembly Engines
Basic Interactive Python Scripting
Simple Interactive Code Block
Source
```{pyodide}
#| autorun: true
print("Hello, Interactive Python!")
```
Output
Python Cell Options Quick Reference
Option | Description | Default |
---|---|---|
autorun |
Automatically executes code on page load. | false |
edit |
Allows editing of code blocks. | true |
runbutton |
Shows “Run Code” button. | true |
completion |
Enables autocomplete suggestions. | true |
startover |
Shows “Start Over” button. | true |
persist |
Persists user modifications. | false |
timelimit |
Time limit for code execution (seconds). | 30 |
include |
Display code block output and source. | true |
Detailed tutorial: Interactive Code Blocks Explained
Python Plotting Basics (Matplotlib, Plotly, Seaborn)
Matplotlib Example
Source
```{pyodide}
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title("Interactive Matplotlib Plot")
plt.show()
```
Output
Plotly Example
Source
```{pyodide}
import plotly.express as px
import pandas as pd
# Sample data
df = pd.DataFrame({
'x': [1, 2, 3, 4, 5],
'y': [10, 20, 15, 25, 30]
})
fig = px.line(df, x='x', y='y', title='Interactive Plotly Line Plot')
fig.show()
```
Output
Seaborn Example
Source
```{pyodide}
import seaborn as sns
import matplotlib.pyplot as plt
# Load dataset
df = sns.load_dataset("iris")
sns.scatterplot(data=df, x="sepal_length", y="sepal_width", hue="species")
plt.title("Interactive Seaborn Plot")
plt.show()
```
Output
Using Pyodide Effectively
Loading Packages
Include Python packages in the document YAML:
---
format: live-html
pyodide:
packages:
- numpy
- matplotlib
- seaborn
---
Access Data From Other Cells
Define data in an OJS variable and import it:
```{pyodide}
#| define:
#| - dataset
import seaborn as sns
dataset = sns.load_dataset("iris").to_dict()
```
```{pyodide}
#| input:
#| - dataset
import pandas as pd
df = pd.DataFrame(dataset)
df.head()
```
Advanced Pyodide Cell Options
Persistence:
#| persist: true
(Saves user edits locally)
Immediate Execution:
#| runbutton: false
(Executes code immediately as it’s typed)
Detailed tutorial: Managing Execution Environments
Further Reading
- Interactive Python for Beginners: Your First Script
- Interactive Python Course: Step-by-Step Lessons
- Integrating Shinylive with Python
Explore More Articles
Here are more articles from the same category to help you dive deeper into the topic.
Reuse
Citation
@online{kassambara2025,
author = {Kassambara, Alboukadel},
title = {Interactive {Python} {Quick} {Reference} {Cheatsheet}},
date = {2025-03-22},
url = {https://www.datanovia.com/learn/interactive/cheatsheets/interactive-python-quick-reference.html},
langid = {en}
}