1

I am using flask_view_counter to track all the views. It saves to the database properly, however, I can not get all the views inside the program. I have tried to do it using SQLAlchemy, but mostly it uses models to find information from database. Here is the example of code:

main.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Views</title>
</head>
<body>
    <h1>All the views: {{ my_views }}</h1>
</body>
</html>

app.py

from flask import Flask, render_template
from flask_view_counter import ViewCounter
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
db = SQLAlchemy(app)
app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///site.db'
views = ViewCounter(app, db)

@app.route('/')
@views.count
def mai():
    my_views = None # how to get it???
    return render_template('main.html', my_views=my_views)

if __name__ == "__main__":
    app.run(debug=True)

If there is another way to make a view counter using python or javascript, I would like to see it.

2 Answers2

0

The flask_view_counter code is fairly simple: it creates a database table and inserts a new record every time your flask view was called. There is no model – or, declarative extension in terms of SQLAlchemy, – as you would expect. To fetch the records, you would need to select from the table directly.

The reference to the table is available as an attribute on the ViewCounter instance. In your case, it would be views.requests. You can pass the table object to sqlalchemy.sql.select and run a query with db.engine.execute.

from flask import Flask, render_template
from flask_view_counter import ViewCounter
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql import select

app = Flask(__name__)
db = SQLAlchemy(app)
app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///site.db'
views = ViewCounter(app, db)

@app.route('/')
@views.count
def mai():
    my_views = db.engine.execute(select([views.requests]))
    return render_template('main.html', my_views=my_views)

if __name__ == "__main__":
    app.run(debug=True)

There are many other options for counting views available, it depends on your use case, of course. As a simple solution, you could use the more intrusive Google Analytics JS library. You could look into counting the views in pure python without caching them. In case you don't want to use SQLAlchemy you could even roll your own view decorator based on the flask_view_counter's source code and store data somewhere else.

Nikolay Shebanov
  • 1,363
  • 19
  • 33
  • Your solution doesn't work. It frows an error: `sqlalchemy.exc.ArgumentError: columns argument to select() must be a Python list or other iterable` – chirikchekmen Dec 02 '20 at 08:55
0

So, the result should be modified from solution above:

app.py

from flask import Flask, render_template
from flask_view_counter import ViewCounter
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql import select

app = Flask(__name__)
db = SQLAlchemy(app)
app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///site.db'
views = ViewCounter(app, db)

@app.route('/')
@views.count
def main():
    func = select([views.requests])
    my_views = db.engine.execute(func)
    return render_template('main.html', my_views=my_views)

if __name__ == "__main__":
    app.run(debug=True)

main.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Views</title>
</head>
<body>
    {% for view in my_views %}
        <h1>All each ip for view: {{ view.ip }}</h1>
    {% endfor %}
</body>
</html>