-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
45 lines (39 loc) · 1.58 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from flask import Flask, render_template, jsonify, request
import requests
app = Flask(__name__)
# ----------STEP 1------------
# The user opens the home page
# ----------------------------
@app.route('/')
def main():
return render_template("main.html")
# ----------------STEP 2------------------
# +++++In HTML template (main.html)+++++++
# The user fills the input box with a word
# ----------------------------------------
# ------------------STEP 3----------------------
# ++++++++In HTML template (main.html)++++++++++
# An AJAX request is sent to the url /test_ajax/
# ----------------------------------------------
@app.route('/test_ajax/', methods=['POST'])
def test():
# -----------------------STEP 4-------------------------
# Data are collected from the request and sent to an API
# ------------------------------------------------------
base_url = "http://fr.wikipedia.org/w/api.php"
params_url = {"action": "opensearch",
"search": request.form["question"],
"limit": "1",
"namespace": "0",
"format": "json"}
liste=requests.get(url=base_url, params=params_url)
# ------------STEP 5--------------------
# API Data are sent back to the template
# --------------------------------------
return jsonify({'data': render_template('reponse.html', liste=liste.json())})
# -------------STEP 6-----------------
# +++In HTML template (main.html)+++++
# div#reponse is updated with API data
# ------------------------------------
if __name__ == "__main__":
app.run(debug=True)