Python Forum
Load JSON Data Over Several Templates
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Load JSON Data Over Several Templates
#1
Summary and Background of What I'm Doing:

I'm creating a Flask quiz app which presents a user with three different options are allows them to input their answer using a form and submit button. I'm storing my key/values in a JSON file and I'm using ginja to load the data to the browser. Here's what the JSON file looks like:

[
{ "question": "What is the Capital of France?",
"option1":"Dublin",
"option2": "Paris",
"option3": "Cork",
"answer": "Paris",
"score": 0
},


{ "question": "What Was Freud's First Name",
"option1":"Fred",
"option2": "James",
"option3": "Sigmund",
"answer": "Sigmund",
"score": 0
}
]


My problem is related to how this data can be pass through several different pages using Flask. I can't seem to load or route this information to the browser and I don't really understand why. Here's my flask route:

@app.route("/")

@app.route('/question')
def question():
    data = []
    with open("data/quiz.json", "r") as json_data:
        data = json.load(json_data)
        return render_template('question1.html', quiz=data)

###This is the route I use to load and read my JSON file before calling my first template titled 'question1.html'###

        
@app.route('/capitals', methods=['POST'])
def capitals():
        answer = request.form["capitals"]
        if answer == "Paris":
            return render_template('welcome.html') 
        else:
            return "<h4>is not correct, guess again.</h4>" 

###This is my first template where I assign some simple logic to match the user's guess with the answer value. If the answer is correct, then the next question template is loaded###
    
@app.route('/welcome', methods=['POST'])
def welcome():
    return render_template('welcome.html')    

###And here's where the problem starts. The first page has the data loaded to the browser, but by the time I get to question two, the data doesn't appear anymore.###
    
HTML Code:

Here's what I wrote in welcome.html:

{% for object in quiz %}



<br>
{{ quiz[1]["question"] }}
<br>
<br>
{{ quiz[1]["option1"] }}
<br>
{{ quiz[1]["option2"] }}
<br>
{{ quiz[1]["option3"] }}
<br>

<form action="/welcome" method='POST'>
Country:
<br>
<input type="text" name="welcome" value="">
<br>
<br>
<input type="submit" value="Submit">
</form>

{% endfor %}


Just in case...

If you need any more information or if I haven't been clear enough or explain this well, please let me know and I'll fix it.

Thank you.
Reply
#2
Can anybody help me out with this one? I'm not expecting an answer, just some pointers on what to do in a situation like this.
Reply
#3
Can you provide a fully runnable example? Ideally minimized to include only code relevant to the problem at hand.
Reply
#4
(Jan-03-2019, 01:00 AM)micseydel Wrote: Can you provide a fully runnable example? Ideally minimized to include only code relevant to the problem at hand.

I'm not entirely sure what you're asking, mate. I'm really just looking for a way to use this function which reads the json file:
def question():
    data = []
    with open("data/quiz.json", "r") as json_data:
        data = json.load(json_data)
        return render_template('question1.html', quiz=data)
in the templates that follow. My problem is that I can read this JSON file into subsequent templates, i.e by the time I get to the welcome.html route, the function doesn't read the data into the template anymore. It reads into the question.html one, but now welcome.html.
Reply
#5
You probably want to load the data outside of the route (otherwise it will be reloaded on every request, and file reads are blocking – only one can be done at a time) to increase the efficiency of your application.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get json data by request with header? dokipo 0 1,912 Nov-06-2021, 04:51 PM
Last Post: dokipo
  Issue with json data pythonFresher 5 3,280 Apr-07-2019, 07:26 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020