・ For those who have started studying Flask and JavaScript -Use TheCatAPI.com: Beta to create an app that retrieves images of cats with Ajax requests.
Prepare Flask. If you're still not sure about Flask, check out the article below. [Probably the second most popular web application framework in Python] Summarize the basics of Flask in an easy-to-understand manner
The configuration is as follows.
app.py
templates
└─hello.html
app.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template('hello.html')
if __name__ == "__main__":
app.run(debug=True)
hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hello</title>
<h1>hello</h1>
</head>
<body>
</body>
</html>
When you start Flask above, the following screen will appear on your browser.
Now, let's display the image of the cat with the Fetch API. Click the button to display the image of the cat.
The entire HTML code is below
.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>hello</title>
<style>
.w300 {
width: 300px;
}
</style>
</head>
<body>
<form id="form">
<div id="image"></div>
<button id="lookCatButton" type="button">See cat</button>
</form>
<script>
const url = 'https://api.thecatapi.com/v1/images/search?size=full';
const options = {
method: 'GET'
}
function getCatImage(url, options){
return fetch(url, options)
.then( response => response.json() );
}
async function getImage(url, options){
const response = await getCatImage(url, options);
const imageDiv = document.getElementById("image");
const imageElement = document.createElement("img");
imageElement.src = response[0].url;
imageElement.className = "w300";
imageDiv.appendChild(imageElement);
}
document.getElementById("lookCatButton")
.addEventListener("click", () =>{
getImage(url, options)
})
</script>
</body>
</html>
With the Fetch API, you can set the request destination ** URL ** and the second ** options ** as the first argument. This time, define the URL and options as follows.
.js
const url = 'https://api.thecatapi.com/v1/images/search?size=full';
const options = {method: 'GET'};
In options, specify GET as the request method. If you want to know more about options, please refer to the following. https://developer.mozilla.org/ja/docs/Web/API/Fetch_API/Using_Fetch https://developer.mozilla.org/ja/docs/Web/API/WindowOrWorkerGlobalScope/fetch
Since the return value of fetch is returned by promise, you can use the **. Then () ** method. I will. And since the callback function can be used in **. Then () **, omit () because there is one ** argument in ** arrow function **, and omit {} because it is the only statement return. **.
.js
fetch(url, options)
.then( response => response.json() );
If you write it with an arrow function that is not omitted, it will be as follows.
.js
fetch(url, options)
.then( (response) => {
return response.json()
});
When using fetch, the code written after it may be executed before the request is sent and the response is returned. Therefore, I use ** asyn / await ** to execute fetch and execute the subsequent code after the response is returned.
Define a ** async function ** and add ** await ** to the function that uses fetch in it and put it in a variable. It is the following part
.js
async function getImage(url, options){
const response = await getCatImage(url, options);
After that, the image URL of the cat is displayed as appendChild.
Since the series of processes is executed after clicking the button, it is executed when ** click ** is done with ** addEventListener **.
.js
document.getElementById("lookCatButton")
.addEventListener("click", () =>{
getImage(url, options)
})
Now, every time you press the button, the image of the cat will be displayed.
As mentioned above, fetch requires the URL to send the request to the first argument, so ** create the URL of the request destination by Flask routing **. Therefore, the code to get the data of Neko-chan API and ** return ** with jsonify to dump with json when returning as a response.
I made the following code. ** / get_data ** is added.
from flask import Flask, render_template, jsonify
import requests
app = Flask(__name__)
@app.route('/get_data')
def get_data():
url = 'https://api.thecatapi.com/v1/images/search?size=full' #Cat API
response = requests.get(url)
cat_img_url = eval(response.text)[0]['url'] #Get the image URL here
response_data = {
'cat_img_url' : cat_img_url
}
return jsonify(response_data) #Return to js side with jsonify
@app.route('/')
def hello_world():
return render_template('hello.html')
if __name__ == "__main__":
app.run(debug=True)
On the JavaScript side, change the URL of the request destination to **. / Get_data ** of the routing destination you set earlier.
.js
const url = './get_data';
Since the dumped value is entered in json from python, get the "cat_img_url" set on the python side.
.js
imageElement.src = response.cat_img_url;
Everything else is the same, and the overall code is as follows.
.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.w300 {
width: 300px;
}
</style>
</head>
<body>
<form id="form">
<div id="image"></div>
<button id="lookCat" type="button">See cat</button>
</form>
<script>
const url = './get_data';
const options = {
method: 'GET'
}
function getCatImage(url, options){
return fetch(url, options)
.then( response => response.json() );
}
async function getImage(url, options){
const response = await getCatImage(url, options);
const imageDiv = document.getElementById("image");
const imageElement = document.createElement("img");
imageElement.src = response.cat_img_url;
imageElement.className = "w300";
imageDiv.appendChild(imageElement);
}
document.getElementById("lookCat")
.addEventListener("click", () =>{
getImage(url, options)
})
</script>
</body>
</html>
Now, when you click the button, you can get the image and display it on the python side.
By the way, if you directly access the routing destination added this time, the following contents will be displayed.
This time, I tried fetch request and response with two patterns, one requesting on the python side and the other requesting on the JavaScript side, which was very good for organizing my head.
Notes on the latest Ajax request communication and Fetch API [Probably the second most popular web application framework in Python] Summarize the basics of Flask in an easy-to-understand manner MDN web docs - Fetch API MDN web docs-Using Fetch MDN web docs --Callback function MDN web docs --Arrow Functions MDN web docs - async function