mardi 7 novembre 2017

Passing objects through Flask URL's

I am working on a site using Flask that is pulling data from an API, processing it into JSON, and then dynamically loading it throughout the site as needed. I am having an issue with matching the URL appropriately while at the same time efficiently loading what data I need.

Here is my main file:

import requests
from flask import Flask, render_template

app = Flask(__name__)

url = 'http://ift.tt/1HxWTaZ'
myHeaders={
  "X-Mashape-Key": 'key-here',
  "Accept": "application/json"
}
r = requests.get(url, headers=myHeaders)
cards = r.json()
badSets = ['Promo', 'Hall of Fame', 'Tavern Brawl', 'Hero Skins', '    Missions', 'Credits', 'System', 'Debug']

@app.route('/')
def index():
    return render_template('index.html', cards=cards, badSets=badSets)


@app.route('/<setName>', methods=['GET'])
def set(setName):
    return render_template('set.html', cards=cards, setName=setName, badSets=badSets)


@app.route('/<setName>/<cardName>', methods=['GET'])
def individualCard(setName, cardName):
    return render_template('card.html', cards=cards, setName=setName, cardName=cardName, badSets=badSets)

First, I'd prefer to not have to pass each html page all of my data. Is there some way to pull only what is needed and ignore the rest?

Second, I would like to pass a dictionary object i.e.

if card['name'] == card[setName][--index--][cardName]:
    pass card object to html page here

How could this be achieved, and is it possible without having to use a for-loop through all of my data?

I'm happy to load my html pages if needed.




Aucun commentaire:

Enregistrer un commentaire