Skip to content

Commit

Permalink
feat: add config editing to the index page
Browse files Browse the repository at this point in the history
Also included the results, this way you can make quick changes to the config and immediately test them. This made the run_simulation api endpoint redundant, so it was removed. The module remains though for easy api extension in the future
  • Loading branch information
chriskelly committed Oct 31, 2023
1 parent a2365d3 commit 7bea33c
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 71 deletions.
30 changes: 26 additions & 4 deletions app/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
"""Flask app definition"""

from flask import Flask, redirect, url_for
from flask import Flask, redirect, render_template, request, url_for
import pandas as pd
from app.models.simulator import SimulationEngine
from app.routes.api import api as api_blueprint


def create_app():
app = Flask(__name__)
app.register_blueprint(api_blueprint, url_prefix="/api")

@app.route("/")
def redirect_to_simulation():
return redirect(url_for("api.run_simulation"))
@app.route("/", methods=["GET", "POST"])
def index():
df = pd.DataFrame()
success_percentage = ""
if request.method == "POST":
edited_config = request.form["edited_config"]
with open("config.yml", "w") as config_file:
config_file.write(edited_config)
if "run_simulation" in request.form:
engine = SimulationEngine()
engine.gen_all_trials()
df = engine.results.as_dataframes()[0]
success_percentage = round(
100 * engine.results.success_rate(), ndigits=1
)
with open("config.yml", "r") as config_file:
config = config_file.read()
return render_template(
"index.html",
config=config,
table=df.to_html(classes="table table-striped"),
success_percentage=success_percentage,
)

return app
17 changes: 1 addition & 16 deletions app/routes/api.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,4 @@
"""API Endpoints"""
from flask import Blueprint, render_template
from app.models.simulator import SimulationEngine
from flask import Blueprint

api = Blueprint("api", __name__)


@api.route("simulation")
def run_simulation():
"""Run the simulation"""
engine = SimulationEngine()
engine.gen_all_trials()
df = engine.results.as_dataframes()[0]
success_percentage = round(100 * engine.results.success_rate(), ndigits=1)
return render_template(
"simulation.html",
table=df.to_html(classes="table table-striped"),
success_percentage=success_percentage,
)
82 changes: 82 additions & 0 deletions app/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!DOCTYPE html>
<html>

<head>
<title>Life Finances</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<style>
.container {
width: 100%;
display: flex;
}

.left-column {
width: 28%;
background-color: #f2f2f2;
padding: 20px;
}

.right-column {
width: 68%;
background-color: #e0e0e0;
padding: 20px;
}

.table-container {
max-height: 450px;
/* Set the maximum height of the table container */
overflow-y: auto;
/* Enable vertical scrolling if the table overflows */
}

table {
border-collapse: collapse;
width: 100%;
}

/* All cell rules */
th,
td {
padding: 8px;
text-align: left;
}

/* Header rules */
th {
position: -webkit-sticky;
/* For Safari */
position: sticky;
top: 0;
background-color: #f2f2f2;
}
</style>
</head>

<body>
<div class="container">
<div class="left-column">
<h1>Edit Config</h1>
<form method="POST">
<input type="submit" name="save" value="Save">
<input type="submit" name="run_simulation" value="Save & Run Simulation">
<textarea name="edited_config" rows="20" cols="40">{{ config }}</textarea><br><br>
</form>
</div>
<div class="right-column">
<h1>Chance of Success: {{success_percentage}}%</h1>
<h1>First Result</h1>
<div class="table-container">
<table class="table table-striped">
{{ table | safe }}
</table>
</div>
</div>
</div>
</body>

<body>

</body>

</html>
50 changes: 0 additions & 50 deletions app/templates/simulation.html

This file was deleted.

1 change: 0 additions & 1 deletion tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def test_routes(client: FlaskClient):
"""Ensure all routes are working"""
routes = [
"/",
"/api/simulation",
]
valid_status_codes = {200, 302}
for route in routes:
Expand Down

0 comments on commit 7bea33c

Please sign in to comment.