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.pyOption 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 jupyterUsing 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 labManual Activation
# Activate environment
source .venv/bin/activate
# Then use quarto normally
quarto render
quarto previewUsing Python in Blog Posts
Basic Python Code Block
import pandas as pd
import numpy as np
# Your Python code here
data = pd.DataFrame({'x': [1, 2, 3], 'y': [4, 5, 6]})
print(data)Python Code Block with Options
#| label: my-analysis
#| echo: true
#| eval: true
#| fig-cap: "My Figure Caption"
import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [4, 5, 6])
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
df = pd.read_csv('cardiac_arrests_cy.csv')
# Get comprehensive statistics
stats = custom_summary(df['call_entry_time'])
print(stats)
# Create visualizations
fig = create_response_time_plots(df, 'call_entry_time')
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
conn_str = "mssql+pyodbc://server/database?driver=ODBC+Driver+17+for+SQL+Server"
# Load data using your SQL queries
df = load_sql_data(conn_str, CARDIAC_ARREST_CURRENT_YEAR)Rendering
To render your site with Python support:
quarto renderTo preview with live reload:
quarto previewThe site will automatically detect and execute both R and Python code blocks based on the language specified in the fenced code block.