-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add config editing to the index page
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
1 parent
a2365d3
commit 7bea33c
Showing
5 changed files
with
109 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters