Adds lightweight request metrics to flask applications
Flask-view-counter is a simple addon for flask applications that lets you track pageloads. It requires flask-sqlalchemy (for now), so if you are already using it, you can get pageload metrics for free. Flask-view-counter provides a decorator for views that records information about the request. Additionally, tools for querying the generated data are provided. Display of the data is left as an exercise to the user, in order to keep dependencies (and the extension) as small and simple as possible. SQLAlchemy must of course be setup and configured properly.
from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_view_counter import ViewCounter app = Flask(__name__) app.config.from_object(select_config()) db = SQLAlchemy(app) view_counter = ViewCounter(app, db) from app import routes
from app import app, view_counter @app.route('/') @view_counter.count def index(): return "Hello World"
Flask-view-counter will then record the decorated view, logging details about the user.
A word of caution, make sure you are decorating a view function, that is, the
function which actually returns a valid wsgi response. The primary place you
will encounter this is in calls to
abort(). In this case, your "view" won't
return anything at all, and instead the error handler will return the response.
Consequently, if you want to know about this error (presumably you do!), then
you must decorate a custom error handler function for that error code. I.E
@app.route(/imight400/<yes>/) @viewcounter.count def might400(yes): if yes == "yes": abort(400) # When we abort here, viewcounter can't see what is # happening, define a custom error handler if you want to # know about this return "OK!" @app.errorhandler(400) @viewcounter.count def bad_request(e): # with the error handler decorated, flask-view-counter will record this # error. You can use any of the return formats that flask allows. return render_template("400.html", error=e), 400
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Hashes for flask_view_counter-0.1.1-py3-none-any.whl