Python Support for Dr. D’s Data Science
This site now supports both R and Python code execution and rendering in Quarto documents, using uv for fast and reliable Python environment management.
Prerequisites
- uv (install from: https://docs.astral.sh/uv/getting-started/installation/)
- Quarto (install from: https://quarto.org/docs/get-started/)
Setup
Option 1: Automated Setup (Recommended)
python setup_python.py
Option 2: Manual Setup
# Create virtual environment
uv venv
# Install all data science packages
uv pip install pandas numpy matplotlib seaborn scipy statsmodels scikit-learn sqlalchemy pyodbc psycopg2-binary folium geopandas jupyter ipykernel python-dotenv requests plotly altair
# Verify Quarto can detect Python
source .venv/bin/activate && quarto check jupyter
Using the Environment
With uv (Recommended)
# Render your site (automatically activates environment)
uv run quarto render
# Preview your site
uv run quarto preview
# Run Jupyter
uv run jupyter lab
Manual Activation
# Activate environment
source .venv/bin/activate
# Then use quarto normally
quarto render
quarto preview
Using Python in Blog Posts
Basic Python Code Block
import pandas as pd
import numpy as np
# Your Python code here
= pd.DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]})
data print(data)
Python Code Block with Options
#| label: my-analysis
#| echo: true
#| eval: true
#| fig-cap: "My Figure Caption"
import matplotlib.pyplot as plt
1, 2, 3], [4, 5, 6])
plt.plot([ plt.show()
Common Code Block Options
#| echo: true/false
- Show/hide code#| eval: true/false
- Execute/don’t execute code
#| output: true/false
- Show/hide output#| warning: false
- Hide warnings#| message: false
- Hide messages#| fig-cap: "Caption"
- Add figure caption#| fig-width: 8
- Set figure width#| fig-height: 6
- Set figure height
Python Utilities
The python_utils.py
file contains Python equivalents of the R functions used in your analyses:
custom_summary()
- Comprehensive statistics (equivalent to your R function)extract_datetime_features()
- Extract date/time componentscreate_response_time_plots()
- Generate analysis plotscreate_geographic_map()
- Interactive maps with Foliumload_sql_data()
- Database connectivity
Example Usage in a Blog Post
#| label: load-utilities
from python_utils import custom_summary, create_response_time_plots
import pandas as pd
# Load your data
= pd.read_csv('cardiac_arrests_cy.csv')
df
# Get comprehensive statistics
= custom_summary(df['call_entry_time'])
stats print(stats)
# Create visualizations
= create_response_time_plots(df, 'call_entry_time')
fig fig.show()
Database Connections
For SQL Server connections (equivalent to your R setup):
import sqlalchemy as sa
from python_utils import load_sql_data, CARDIAC_ARREST_CURRENT_YEAR
# Connection string for SQL Server
= "mssql+pyodbc://server/database?driver=ODBC+Driver+17+for+SQL+Server"
conn_str
# Load data using your SQL queries
= load_sql_data(conn_str, CARDIAC_ARREST_CURRENT_YEAR) df
Rendering
To render your site with Python support:
quarto render
To preview with live reload:
quarto preview
The site will automatically detect and execute both R and Python code blocks based on the language specified in the fenced code block.