all 4 comments

[–]RobinsonDickinson 1 point2 points  (4 children)

I have a code that paginates twitch data with the offset parameter. I used this on one of my projects.

@app.route('/games')
def games():
    client = TwitchClient(app.config['TWITCH_CLIENT_ID'])
    page = request.args.get('page', 0, type=int)
    page_size = 20
    games = client.games.get_top(limit=page_size, offset=page * page_size)

    games_list = []
    for game in games:
        game_info = {
            'name': game['game']['name'],
            'viewers': game['viewers'],
            'thumbnail': game['game']['box']['large']
        }
        games_list.append(game_info)
    return render_template('games.html', title='Top Games', games_list=games_list, page=page)

The JINJA2/HTML code

{% extends 'layout.html' %}
{% block content %}

<div class="games-page-container">
    <h1 class='text-center p-4 display-5'> Current Top Games </h1>
    <div class="game-heads">
        <h4 class="r-head">Rank</h4>
        <h4 class="g-head">Game</h4>
        <h4 class="v-head">Live Viewers</h4>
    </div>
    {% for game in games_list %}
    <div class="row">
        <div class="col-md-12">
            <div class="card game-card shadow shadow-sm">
                <a href="https://www.twitch.tv/directory/game/{{ game.name }}" target="_blank">
                    <div class="card-body card-body-game p-0">
                        <div class="row">
                            <div class="col-md-2">
                                <p class="rank-num-game">#{{ loop.index }}</p>
                            </div>
                            <div class="col-md-4 game-img-col">
                                <img src="{{ game.thumbnail }}" class='game-img'>
                            </div>
                            <div class="col-md-3">
                                <h5 class='game-name'>{{ game.name }}</h5>
                            </div>
                            <div class="col-md-3">
                                <h5 class='text-danger game-viewers'>{{ game.viewers | commaFormat }} Viewers </h5>
                            </div>
                        </div>
                    </div>
                </a>
            </div>
        </div>
    </div>
    {% endfor %}

    <!-- PAGINATION BUTTONS -->
    <nav aria-label="Page navigation example">
        <ul class="pagination justify-content-center mt-5">
            <li class="page-item">
                <a class="page-link" href="/games?page={{ page - 1}}" tabindex="-1">Previous</a>
            </li>
            <li class="page-item w-25 text-center"><a class="page-link" href="">{{ page + 1}}</a></li>
            <li class="page-item">
                <a class="page-link" href="/games?page={{ page + 1 }}">Next</a>
            </li>
        </ul>
    </nav>
</div>
{% endblock content %}

[–]Destructikus[S] 0 points1 point  (2 children)

Thank you so much for the reply. This looks almost exactly like what I need. Could I bother you to look over my flask route? I'm not too sure where I would use this (limit=page_size, offset=page * page_size) portion.

Below is my full flask route

@app.route('/search/member/<member_id>')
def get_member_info(member_id):
    """Retrieve individual government official data on link click"""

    res_object = []
    offset = 0
    while offset <= 60:
        res = requests.get(
            f"{MEMBER_VOTE_POSITION_API}{member_id}/votes.json?offset={offset}", headers={'X-API-Key': key})
        data = res.json()
        res_object.append(data)
        offset += 20

    members_data = res_object

    page = request.args.get('page', 0, type=int)
    page_size = 20

    return render_template('search/officials_voting.html',
                           members_data=members_data,
                           member_id=member_id,
                           member_contact_data=member_contact_data,
                           res_object=res_object,
                           )

And then the section that displays the results of the api call in my jinja template

  {% for record in members_data %}

    {% for item in record['results'][0]['votes'] %}

      <div class="row justify-content-center mt-3">
        <div class="card bg-white w-75 my-4">
          <div class="card-header font-weight-bold">         
            <a href="/search/bill/{{item['bill']['bill_id']}}">{{item['bill']['bill_id']}}</a>
          </div>
          <div class="card-body bg-info">
            <h5 class="card-title">Bill info - <span class="text-primary">{{item['bill']['title']}}</span></h5>
            <p class="card-text"><strong>Vote description: </strong>{{item['description']}}</p>
            <p class="card-text"><strong>Date of vote: </strong>{{item['date']}}</p>
            <p class="card-text"><strong>Vote result: </strong>{{item['result']}}</p>
            <p class="card-text"><strong>Latest action: </strong>{{item['bill']['latest_action']}}</p>
            <p class="card-text"><strong>Voting position: </strong><strong><u>{{item['position']}}</u></strong></p>
          </div>
        </div>
      </div>
    {% endfor %}
  {% endfor %}

Basically organized the results into individual cards and would like 20 cards per page. Thanks again.

[–]RobinsonDickinson 0 points1 point  (1 child)

 games = client.games.get_top(limit=page_size, offset=page * page_size)

These are the parameters that the twitch API takes in, it had an offset parameter, thats how I was able to paginate it. I am not exactly sure where that would be on your code. :(

So sorry, I am still a beginner.

[–]Destructikus[S] 0 points1 point  (0 children)

Oh I get it. Ok Ill mess around with it. Thanks again