Building a Harford County Accident Tracker with Flask and Folium
Words
334
Reading
2 min
Listen
Play
1y
I recently set up a server on IONOS to create a simple yet effective local accident-reporting web application. This tool allows community members to easily report traffic accidents and visualize them in real-time on an interactive map.
🚧 Why This Matters
Real-time accident reporting helps locals avoid traffic congestion, improves community safety, and raises awareness about accident-prone areas.
⚙️ Technologies Used
- Python (Flask): Lightweight web framework
- Folium: Interactive mapping in Python
- Simple Text Storage: For ease and simplicity (
accident_reports.txt)
🛠️ Application Features
🔸 Report Submission
Residents submit accident reports via a straightforward web form.
🔸 Report Storage
Reports are stored in a local file for easy retrieval.
🔸 Interactive Mapping
Folium dynamically visualizes accident locations.
📌 Core Application Code
Below is the full, self-contained Flask application code that handles accident reporting, storage, and map visualization:
from flask import Flask, render_template_string, request, redirect, url_for
import folium
import os
app = Flask(__name__)
REPORTS_FILE = 'accident_reports.txt'
# Load all accident reports
def load_reports():
if not os.path.exists(REPORTS_FILE):
return []
with open(REPORTS_FILE, 'r') as f:
lines = f.readlines()
reports = []
for line in lines:
try:
lat, lng, desc = line.strip().split('|', 2)
reports.append((float(lat), float(lng), desc))
except:
continue
return reports
# Save new accident report
def save_report(lat, lng, desc):
with open(REPORTS_FILE, 'a') as f:
f.write(f"{lat}|{lng}|{desc}\n")
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
lat = request.form.get('lat')
lng = request.form.get('lng')
desc = request.form.get('description')
if lat and lng and desc:
save_report(lat, lng, desc)
return redirect(url_for('index'))
# Generate interactive map
m = folium.Map(location=[39.5362, -76.3526], zoom_start=11)
for lat, lng, desc in load_reports():
folium.Marker([lat, lng], popup=desc).add_to(m)
map_html = m._repr_html_()
# Simple HTML interface
html = '''
Harford County Accident Map
Report an Accident
Submit Report
{{ map_html|safe }}
'''
return render_template_string(html, map_html=map_html)
if __name__ == '__main__':
app.run(debug=True)