from flask import Flask, render_template_string, request
import requests
app = Flask(__name__)
FASTAPI_URL = "http://127.0.0.1:8000/search"
HTML = """
<form method="GET" action="/">
<!-- Саша -->
<input type="text" name="name" placeholder="Введите имя...">
<button type="submit">Найти</button>
</form>
<ul>
{% for p in people %}
<li>{{ p.name }} — город: {{ p.city }}</li>
{% endfor %}
</ul>
"""
@app.route('/')
def index():
name = request.args['name']
#! people = [{"name":"Мария", "city":"Београд"},{"name":"Мария", "city":"Техас"}]
#! данный список с людьми будет выводиться на наш экран с помощью цикла for
people = []
if name:
try:
#* http://127.0.0.1:8000/search/name=Мария
response = requests.get(FASTAPI_URL, params={'name': name})
#* {"Мария":"Москва"}
people = response.json()
except:
print("Ошибка")
return render_template_string(HTML, people=people)
if __name__ == '__main__':
app.run(port=5000)