From 0dcd6ace5919332559a31908bd72d52b5fee827c Mon Sep 17 00:00:00 2001 From: SathvikPalley Date: Wed, 30 Apr 2025 16:01:06 -0500 Subject: [PATCH 1/2] Add toggle button for DEBUG_TB_INTERCEPT_REDIRECTS to debug toolbar UI --- example/app.py | 35 +++++++++------------ example/test.db | Bin 0 -> 12288 bytes src/flask_debugtoolbar/__init__.py | 7 +++++ src/flask_debugtoolbar/templates/base.html | 22 +++++++++++++ 4 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 example/test.db diff --git a/example/app.py b/example/app.py index a466be6..eda5f98 100644 --- a/example/app.py +++ b/example/app.py @@ -1,50 +1,43 @@ -# Run using: `FLASK_DEBUG=True flask run` +# Run using: `flask run` after setting env vars -from flask import Flask -from flask import redirect -from flask import render_template -from flask import url_for +from flask import Flask, redirect, render_template, url_for from flask_sqlalchemy import SQLAlchemy - from flask_debugtoolbar import DebugToolbarExtension +import os app = Flask(__name__) -app.config["DEBUG_TB_INTERCEPT_REDIRECTS"] = True -# app.config['DEBUG_TB_PANELS'] = ( -# 'flask_debugtoolbar.panels.headers.HeaderDebugPanel', -# 'flask_debugtoolbar.panels.logger.LoggingPanel', -# 'flask_debugtoolbar.panels.timer.TimerDebugPanel', -# ) -# app.config['DEBUG_TB_HOSTS'] = ('127.0.0.1', '::1' ) + +# Use a cross-platform-safe DB path +basedir = os.path.abspath(os.path.dirname(__file__)) +app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join(basedir, "test.db") + app.config["SECRET_KEY"] = "asd" app.config["SQLALCHEMY_RECORD_QUERIES"] = True -app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:////tmp/test.db" -# This is no longer needed for Flask-SQLAlchemy 3.0+, if you're using 2.X you'll -# want to define this: -# app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False +app.config["DEBUG_TB_INTERCEPT_REDIRECTS"] = True +app.config["DEBUG"] = True # ✅ Ensure app runs in debug mode +app.debug = True # ✅ Double-safe flag for debug db = SQLAlchemy(app) toolbar = DebugToolbarExtension(app) - +# Model class ExampleModel(db.Model): __tablename__ = "examples" value = db.Column(db.String(100), primary_key=True) - +# Routes @app.route("/") def index(): app.logger.info("Hello there") ExampleModel.query.get(1) return render_template("index.html") - @app.route("/redirect") def redirect_example(): response = redirect(url_for("index")) response.set_cookie("test_cookie", "1") return response - +# Create the database with app.app_context(): db.create_all() diff --git a/example/test.db b/example/test.db new file mode 100644 index 0000000000000000000000000000000000000000..0b406554fc45bf5fcbd923b06db7c98e38e44907 GIT binary patch literal 12288 zcmeI#!AiqG5P;#`C~5;Gz4a8Bn+b@v_yWcduo$bxD3l&T)CiT-s!gR=pUzkEXf}{@ z&z65+cQV=Czw@4K%4KCJS`vMAR1{JkueZMMQB z3erf@;5?3`eJh^b`O3>xArC None: def render(self, template_name: str, context: dict[str, t.Any]) -> str: template = self.jinja_env.get_template(template_name) return template.render(**context) + +@module.route("/toggle_redirect_intercept", methods=["POST"]) +def toggle_redirect_intercept() -> Response: + current = current_app.config.get("DEBUG_TB_INTERCEPT_REDIRECTS", True) + new_value = not current + current_app.config["DEBUG_TB_INTERCEPT_REDIRECTS"] = new_value + return {"intercept_redirects": new_value} diff --git a/src/flask_debugtoolbar/templates/base.html b/src/flask_debugtoolbar/templates/base.html index 13e10f6..ddd90a0 100644 --- a/src/flask_debugtoolbar/templates/base.html +++ b/src/flask_debugtoolbar/templates/base.html @@ -10,6 +10,13 @@
    {% if panels %}
  1. Hide »
  2. +
  3. + +
  4. {% else %}
  5. DEBUG
  6. {% endif %} @@ -56,4 +63,19 @@

    {{ panel.title()|safe }}

    {% endif %} {% endfor %}
    + From 5cd49958b3fc2bf9aaa564192c33456adc0afd8b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Apr 2025 21:17:20 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- example/app.py | 15 ++++++++++++--- src/flask_debugtoolbar/__init__.py | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/example/app.py b/example/app.py index eda5f98..23a9253 100644 --- a/example/app.py +++ b/example/app.py @@ -1,9 +1,14 @@ # Run using: `flask run` after setting env vars -from flask import Flask, redirect, render_template, url_for +import os + +from flask import Flask +from flask import redirect +from flask import render_template +from flask import url_for from flask_sqlalchemy import SQLAlchemy + from flask_debugtoolbar import DebugToolbarExtension -import os app = Flask(__name__) @@ -15,16 +20,18 @@ app.config["SQLALCHEMY_RECORD_QUERIES"] = True app.config["DEBUG_TB_INTERCEPT_REDIRECTS"] = True app.config["DEBUG"] = True # ✅ Ensure app runs in debug mode -app.debug = True # ✅ Double-safe flag for debug +app.debug = True # ✅ Double-safe flag for debug db = SQLAlchemy(app) toolbar = DebugToolbarExtension(app) + # Model class ExampleModel(db.Model): __tablename__ = "examples" value = db.Column(db.String(100), primary_key=True) + # Routes @app.route("/") def index(): @@ -32,12 +39,14 @@ def index(): ExampleModel.query.get(1) return render_template("index.html") + @app.route("/redirect") def redirect_example(): response = redirect(url_for("index")) response.set_cookie("test_cookie", "1") return response + # Create the database with app.app_context(): db.create_all() diff --git a/src/flask_debugtoolbar/__init__.py b/src/flask_debugtoolbar/__init__.py index 0a1fef0..04ed72b 100644 --- a/src/flask_debugtoolbar/__init__.py +++ b/src/flask_debugtoolbar/__init__.py @@ -358,6 +358,7 @@ def render(self, template_name: str, context: dict[str, t.Any]) -> str: template = self.jinja_env.get_template(template_name) return template.render(**context) + @module.route("/toggle_redirect_intercept", methods=["POST"]) def toggle_redirect_intercept() -> Response: current = current_app.config.get("DEBUG_TB_INTERCEPT_REDIRECTS", True)