[Python] Trying to figure out how to biuld flask page but get lost there

Discussão em 'Python' iniciado por Stack, Novembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    Want to practice a bit with flask, trying to make a hidden interests extractor from meta.

    But getting lost while trying to show table on a page with all interests were find after search.

    I can't show it in a different pages, all results shows on the same page and super hard to read :)

    Here is a python flask app:

    from flask import Flask, request, render_template, redirect, url_for
    from meta_hidden_interests_extractor import get_hidden_interests

    app = Flask(__name__)

    @app.route('/', methods=['GET', 'POST'])
    def index():
    search_key = request.args.get('search_key', '')
    page = int(request.args.get('page', 1))
    per_page = 100

    if request.method == 'POST':
    search_key = request.form['search_key']
    return redirect(url_for('index', search_key=search_key, page=1))

    if not search_key:
    return render_template('index.html', search_key=search_key, page=page, total_pages=0, data=[])

    data = get_hidden_interests(search_key, page, per_page)

    total_items = len(data)
    total_pages = (total_items // per_page) + (1 if total_items % per_page > 0 else 0)

    return render_template('index.html', data=data, search_key=search_key, page=page, total_pages=total_pages)


    if __name__ == '__main__':
    app.run(debug=True, port=5100)


    Here is HTML template:

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Meta Hidden Interests</title>
    <link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
    </head>
    <body>
    <div class="container">
    <h1>Enter Search Key for Meta Hidden Interests</h1>
    <form method="POST">
    <input type="text" name="search_key" placeholder="Enter search key" value="{{ search_key }}" required>
    <button type="submit">Search</button>
    </form>

    {% if data %}
    <div class="table-container">
    <table class="table">
    <thead>
    <tr>
    <th>ID</th>
    <th>Name</th>
    <th>Audience</th>
    <th>Path</th>
    <th>Topic</th>
    </tr>
    </thead>
    <tbody>
    {% for item in data %}
    <tr>
    <td>{{ item.id }}</td>
    <td>{{ item.name }}</td>
    <td>{{ item.audience }}</td>
    <td>{{ item.path }}</td>
    <td>{{ item.topic }}</td>
    </tr>
    {% endfor %}
    </tbody>
    </table>
    </div>

    <div class="pagination">
    {% if page > 1 %}
    <a href="?page={{ page - 1 }}&search_key={{ search_key }}"><button>Previous</button></a>
    {% endif %}
    <span>Page {{ page }} of {{ total_pages }}</span>
    {% if page < total_pages %}
    <a href="?page={{ page + 1 }}&search_key={{ search_key }}"><button>Next</button></a>
    {% endif %}
    </div>
    {% else %}
    <p>No data available. Please enter a search key and try again.</p>
    {% endif %}
    </div>
    </body>
    </html>


    I tryied to create pagination, but withut success. Never work with flask before. Super low skill in python as well :) Still learning. GPT halped me a lot, but can't sovle exact this issue. He offer me thousand variants of code, but non of them works as expected.

    Continue reading...

Compartilhe esta Página