CoolFace
Apppublic

fmazzoni/boston311

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
App README

Boston 311 Service Requests Dashboard

An interactive geospatial dashboard for exploring Boston's 311 service request data from 2011-2025, built with modern Python data visualization technologies.

![Open in Hugging Face Spaces](https://huggingface.co/spaces/fmazzoni/boston311)

Dashboard Preview Python Panel DuckDB

๐ŸŽฏ Features

  • โ€”๐Ÿ—บ๏ธ Interactive Geospatial Visualization - GPU-accelerated mapping with Lonboard
  • โ€”๐Ÿ“Š Dynamic Filtering - Time periods, neighborhoods, sources, and service types
  • โ€”๐ŸŽจ Color-coded Analysis - Visual patterns by category with interactive legends
  • โ€”๐Ÿ“‹ Data Selection - Click points or select areas to view detailed records
  • โ€”โšก High Performance - DuckDB for fast analytics on large datasets
  • โ€”๐Ÿ“ฑ Modern UI - Responsive design with dark theme

๐Ÿ—๏ธ Architecture

The application follows a modular architecture with clear separation of concerns:

src/boston311/
โ”œโ”€โ”€ app.py              # Main application entry point
โ”œโ”€โ”€ config.py           # Centralized configuration
โ”œโ”€โ”€ dashboard.py        # Main StateViewer dashboard component
โ”œโ”€โ”€ database.py         # Database operations and data fetching
โ”œโ”€โ”€ time_periods.py     # Dynamic time period utilities
โ”œโ”€โ”€ color_mapping.py    # Color mapping and visualization utilities
โ”œโ”€โ”€ sql_utils.py        # Safe SQL query construction
โ”œโ”€โ”€ ui_components.py    # Reusable UI components
โ”œโ”€โ”€ logging_utils.py    # Logging configuration utilities
โ””โ”€โ”€ extract.py          # Data preprocessing script

Key Design Principles

  • โ€”๐Ÿ”’ Security First - SQL injection prevention with parameterized queries
  • โ€”โšก Performance - Cached operations and GPU acceleration
  • โ€”๐Ÿงช Testability - Modular components with clear interfaces
  • โ€”๐Ÿ“ Type Safety - Comprehensive type hints throughout
  • โ€”๐Ÿ”„ Fail-Fast - Standard Python exceptions for clear error handling

๐Ÿš€ Quick Start

Prerequisites

  • โ€”Python 3.11+
  • โ€”UV package manager (recommended) or pip

Installation

  1. 1.Clone the repository
bash
   git clone <repository-url>
   cd boston311
  1. 1.Install dependencies
bash
   # Using UV (recommended)
   uv sync
   
   # Or using pip
   pip install -e .
  1. 1.Extract and prepare data (see Data Extraction section below)
  1. 1.Run the dashboard
bash
   # Using UV
   uv run panel serve src/boston311/app.py --show --autoreload
   
   # Or using Python directly
   python -m panel serve src/boston311/app.py --show --autoreload

๐Ÿ“Š Data Extraction

The dashboard requires Boston 311 service request data to be preprocessed from the city's open data portal.

Automated Data Extraction

The extract.py script automatically downloads and processes the data:

bash
# Run the extraction script
uv run python src/boston311/extract.py

# This will:
# 1. Scrape https://data.boston.gov/dataset/311-service-requests
# 2. Download all available CSV files (2011-2025)
# 3. Convert to Parquet format with spatial geometry processing
# 4. Save to data/raw/ directory

Data Processing Steps

  1. 1.๐ŸŒ Web Scraping - Automatically discovers CSV download URLs from the Boston data portal
  2. 2.๐Ÿ“ฅ Download - Fetches CSV files for each year (2011-2025)
  3. 3.๐Ÿ—บ๏ธ Spatial Processing - Converts WKB geometry to PostGIS-compatible format using DuckDB spatial extension
  4. 4.๐Ÿ’พ Parquet Conversion - Saves as efficient Parquet files for fast analytics
  5. 5.๐Ÿงน Data Cleaning - Filters out records without valid geometry

Manual Data Setup

If you prefer to download data manually:

  1. 1.Visit Boston 311 Service Requests
  2. 2.Download CSV files for desired years
  3. 3.Place files in data/raw/ directory
  4. 4.Run the extraction script to convert to Parquet format

Data Schema

The processed data includes these key fields:

  • โ€”`open_dt` - Request submission timestamp
  • โ€”`source` - How the request was submitted (App, Phone, etc.)
  • โ€”`subject` - Type of service request
  • โ€”`neighborhood` - Boston neighborhood
  • โ€”`geometry` - Spatial coordinates (Point geometry)

๐ŸŽ›๏ธ Configuration

All configuration is centralized in config.py:

python
# UI Settings
MAP_HEIGHT = 600
TABLE_HEIGHT = 400
MAX_DISPLAY_RECORDS = 100

# Performance Limits  
MAX_SELECTION_RECORDS = 1000

# Color Schemes
DEFAULT_POINT_COLOR = (255, 140, 0, 255)

๐Ÿ”ง Development

Project Structure

  • โ€”`config.py` - Application configuration and constants
  • โ€”`database.py` - Database operations and data fetching with caching
  • โ€”`time_periods.py` - Dynamic time period generation (relative dates)
  • โ€”`color_mapping.py` - Stable color mapping for categorical data
  • โ€”`sql_utils.py` - Safe SQL query construction (injection prevention)
  • โ€”`ui_components.py` - Reusable UI components (tables, legends)
  • โ€”`logging_utils.py` - Centralized logging configuration
  • โ€”`dashboard.py` - Main StateViewer class with all interaction logic
  • โ€”`extract.py` - Data preprocessing and extraction utilities
  • โ€”`app.py` - Application entry point and Panel template setup

Code Quality

  • โ€”Type Safety - Full type hints with mypy compatibility
  • โ€”Linting - Ruff for fast Python linting
  • โ€”Security - SQL injection prevention with parameterized queries
  • โ€”Performance - Cached database operations and efficient data structures
  • โ€”Modularity - Clear separation of concerns for maintainability

Adding New Features

  1. 1.Data Filters - Add new filter options in database.py and update dashboard.py
  2. 2.Visualizations - Extend color_mapping.py for new color schemes
  3. 3.UI Components - Add reusable components in ui_components.py
  4. 4.Configuration - Add new settings to config.py

๐Ÿ“ˆ Performance

  • โ€”โšก DuckDB - Columnar analytics engine for fast queries on large datasets
  • โ€”๐ŸŽฎ GPU Acceleration - Lonboard leverages WebGL for smooth map rendering
  • โ€”๐Ÿ’พ Caching - Panel caching for database operations and UI components
  • โ€”๐Ÿ“ฆ Parquet - Efficient columnar storage format
  • โ€”๐Ÿ”„ Lazy Loading - Data loaded on-demand based on user selections

๐Ÿ™ Acknowledgments

  • โ€”City of Boston - For providing open access to 311 service request data
  • โ€”Panel - For the excellent dashboard framework
  • โ€”DuckDB - For high-performance analytics capabilities
  • โ€”Lonboard - For GPU-accelerated geospatial visualization

๐Ÿ”— Links