I have refactored the SQL Explorer functionality into its own blueprint (routes/sql_explorer.py) with a /sql URL prefix. This involved moving the relevant routes from app.py, registering the new blueprint, removing the old routes, updating url_for calls in the templates, and documenting the change in the changelog.

Here is a conventional commit message summarizing the changes:

```
feat: Refactor SQL Explorer into blueprint

- Moved SQL Explorer routes (view explorer, save/load/execute/delete queries, view schema, plot queries) from `app.py` into a new blueprint at `routes/sql_explorer.py`.
- Added `/sql` URL prefix to the blueprint.
- Registered the new `sql_explorer_bp` blueprint in `app.py`.
- Removed the original SQL Explorer route definitions from `app.py`.
- Updated `url_for` calls in relevant templates (`sql_explorer.html`, `partials/sql_explorer/sql_query.html`, `base.html`) to reference the new blueprint endpoints (e.g., `sql_explorer.sql_explorer`).
- Updated `templates/changelog/changelog.html` to document this refactoring.
```
This commit is contained in:
Peter Stockings
2025-03-31 23:00:54 +11:00
parent eaeb4ab2c8
commit a8fe28339b
8 changed files with 248 additions and 288 deletions

74
app.py
View File

@@ -10,6 +10,7 @@ from routes.changelog import changelog_bp
from routes.calendar import calendar_bp # Import the new calendar blueprint
from routes.notes import notes_bp # Import the new notes blueprint
from routes.workout import workout_bp # Import the new workout blueprint
from routes.sql_explorer import sql_explorer_bp # Import the new SQL explorer blueprint
from extensions import db
from utils import convert_str_to_date, generate_plot
from flask_htmx import HTMX
@@ -40,6 +41,7 @@ app.register_blueprint(changelog_bp, url_prefix='/changelog')
app.register_blueprint(calendar_bp) # Register the calendar blueprint
app.register_blueprint(notes_bp) # Register the notes blueprint
app.register_blueprint(workout_bp) # Register the workout blueprint
app.register_blueprint(sql_explorer_bp) # Register the SQL explorer blueprint (prefix defined in blueprint file)
@app.after_request
def response_minify(response):
@@ -306,78 +308,6 @@ def delete_exercise(exercise_id):
db.exercises.delete_exercise(exercise_id)
return ""
@app.route("/sql_explorer", methods=['GET'])
def sql_explorer():
saved_queries = db.sql_explorer.list_saved_queries()
if htmx:
return render_block(app.jinja_env, 'sql_explorer.html', 'content', saved_queries=saved_queries)
return render_template('sql_explorer.html', saved_queries=saved_queries)
@app.route("/sql_query", methods=['POST'])
def sql_query():
query = request.form.get('query')
title = request.form.get('title')
error = db.sql_explorer.save_query(title, query)
saved_queries = db.sql_explorer.list_saved_queries()
return render_template('partials/sql_explorer/sql_query.html',
title=title,
query=query,
error=error,
saved_queries=saved_queries)
@app.route("/sql_query/execute", methods=['POST'])
def execute_sql_query():
query = request.form.get('query')
(results, columns, error) = db.sql_explorer.execute_sql(query)
return render_template('partials/sql_explorer/results.html',
results=results,
columns=columns,
error=error)
@app.route('/load_sql_query/<int:query_id>', methods=['GET'])
def load_sql_query(query_id):
(title, query) = db.sql_explorer.get_saved_query(query_id)
saved_queries = db.sql_explorer.list_saved_queries()
return render_template('partials/sql_explorer/sql_query.html',
title=title,
query=query,
saved_queries=saved_queries)
@app.route('/delete_sql_query/<int:query_id>', methods=['DELETE'])
def delete_sql_query(query_id):
db.sql_explorer.delete_saved_query(query_id)
saved_queries = db.sql_explorer.list_saved_queries()
return render_template('partials/sql_explorer/sql_query.html',
title="",
query="",
saved_queries=saved_queries)
@ app.route("/sql_schema", methods=['GET'])
def sql_schema():
schema_info = db.sql_explorer.get_schema_info()
mermaid_code = db.sql_explorer.generate_mermaid_er(schema_info)
create_sql = db.sql_explorer.generate_create_script(schema_info)
return render_template('partials/sql_explorer/schema.html', mermaid_code=mermaid_code, create_sql=create_sql)
@app.route("/plot/<int:query_id>", methods=['GET'])
def plot_query(query_id):
(title, query) = db.sql_explorer.get_saved_query(query_id)
#(results, columns, error) = db.sql_explorer.execute_sql(query)
results_df = db.read_sql_as_df(query)
plot_div = generate_plot(results_df, title)
return plot_div
@app.route("/plot/show", methods=['POST'])
def plot_unsaved_query(): # Rename?
query = request.form.get('query')
title = request.form.get('title')
results_df = db.read_sql_as_df(query)
plot_div = generate_plot(results_df, title)
return plot_div
def get_routes():
routes = []
for rule in app.url_map.iter_rules():