Files
capitalism-eats-the-world/app/routes/main.py

149 lines
4.1 KiB
Python

"""
Main Flask Routes
Handles the primary web interface for the Markov economics simulation,
including the main simulation page and basic navigation.
"""
from flask import Blueprint, render_template, request, jsonify
from app.models import SimulationManager, SimulationParameters
main_bp = Blueprint('main', __name__)
# Global simulation manager instance
simulation_manager = SimulationManager()
@main_bp.route('/')
def index():
"""
Main simulation interface page.
Returns:
Rendered HTML template with simulation controls
"""
# Default parameters for display
default_params = {
'r_rate': 0.05, # 5% capital return rate
'g_rate': 0.03, # 3% economic growth rate
'initial_capital': 1000.0,
'initial_consumption': 1000.0,
'num_agents': 100,
'iterations': 1000
}
return render_template('simulation.html',
default_params=default_params,
title="Markov Economics - Capitalism Eats the World")
@main_bp.route('/simulation')
def simulation():
"""
Alternative route to the main simulation page.
Returns:
Rendered HTML template with simulation controls
"""
return index()
@main_bp.route('/about')
def about():
"""
Information page about the economic theory behind the simulation.
Returns:
Rendered HTML template with theoretical background
"""
return render_template('about.html',
title="About - Markov Economics Theory")
@main_bp.route('/results/<simulation_id>')
def results(simulation_id):
"""
Display results for a specific simulation.
Args:
simulation_id: Unique identifier for the simulation
Returns:
Rendered results template or 404 if simulation not found
"""
simulation = simulation_manager.get_simulation(simulation_id)
if not simulation:
return render_template('error.html',
error_message=f"Simulation {simulation_id} not found",
title="Simulation Not Found"), 404
# Get simulation summary data
latest_snapshot = simulation.get_latest_snapshot()
iterations, total_wealth, gini_coefficients = simulation.get_wealth_evolution()
summary_data = {
'simulation_id': simulation_id,
'parameters': {
'r_rate': simulation.parameters.r_rate,
'g_rate': simulation.parameters.g_rate,
'num_agents': simulation.parameters.num_agents,
'iterations': simulation.parameters.iterations
},
'current_iteration': simulation.current_iteration,
'total_iterations': len(simulation.snapshots),
'latest_snapshot': latest_snapshot,
'has_data': len(simulation.snapshots) > 0
}
return render_template('results.html',
simulation_data=summary_data,
title=f"Results - Simulation {simulation_id[:8]}")
@main_bp.route('/health')
def health_check():
"""
Simple health check endpoint.
Returns:
JSON response indicating service status
"""
return jsonify({
'status': 'healthy',
'service': 'markov-economics',
'active_simulations': len(simulation_manager.active_simulations),
'total_simulations': len(simulation_manager.simulations)
})
@main_bp.errorhandler(404)
def not_found_error(error):
"""
Handle 404 errors with custom template.
Args:
error: The error object
Returns:
Rendered error template
"""
return render_template('error.html',
error_message="Page not found",
title="Page Not Found"), 404
@main_bp.errorhandler(500)
def internal_error(error):
"""
Handle 500 errors with custom template.
Args:
error: The error object
Returns:
Rendered error template
"""
return render_template('error.html',
error_message="Internal server error occurred",
title="Server Error"), 500