-
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.
Cleans up the create_app function. Also cleaned up the IndexPage functionalty in general, extracting functions into `simulator.py` and `config.py` and created tests
- Loading branch information
1 parent
7bea33c
commit b5310e5
Showing
7 changed files
with
180 additions
and
57 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,37 +1,18 @@ | ||
"""Flask app definition""" | ||
|
||
from flask import Flask, redirect, render_template, request, url_for | ||
import pandas as pd | ||
from app.models.simulator import SimulationEngine | ||
from flask import Flask, request | ||
from app.routes.api import api as api_blueprint | ||
from app.routes.index import IndexPage | ||
|
||
|
||
def create_app(): | ||
"""Create the Flask app with index route""" | ||
app = Flask(__name__) | ||
app.register_blueprint(api_blueprint, url_prefix="/api") | ||
|
||
@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, | ||
) | ||
index_page = IndexPage(request) | ||
return index_page.template | ||
|
||
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
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
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,45 @@ | ||
""" | ||
This module contains the IndexPage class, which represents the index page of the LifeFinances app. | ||
It also contains functions for reading and writing configuration files, and generating simulation results. | ||
""" | ||
|
||
from flask import Request, render_template | ||
from app.models.config import read_config_file, write_config_file | ||
from app.models.simulator import gen_simulation_results | ||
|
||
|
||
class IndexPage: | ||
""" | ||
A class representing the index page of the LifeFinances app. | ||
Attributes: | ||
template (str): The HTML template for the index page. | ||
""" | ||
|
||
def __init__(self, req: Request): | ||
self._first_results_table = "" | ||
self._success_percentage = "" | ||
if req.method == "POST": | ||
self._handle_form(req.form) | ||
self._config = read_config_file() | ||
|
||
@property | ||
def template(self): | ||
"""Render the index page template""" | ||
return render_template( | ||
"index.html", | ||
config=self._config, | ||
first_results_table=self._first_results_table, | ||
success_percentage=self._success_percentage, | ||
) | ||
|
||
def _handle_form(self, form: dict[str, str]): | ||
write_config_file(form["edited_config"]) | ||
if "run_simulation" in form: | ||
self._update_simulation_results() | ||
|
||
def _update_simulation_results(self): | ||
results = gen_simulation_results() | ||
first_results = results.as_dataframes()[0] | ||
self._first_results_table = first_results.to_html(classes="table table-striped") | ||
self._success_percentage = results.calc_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
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
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