I want the temperature to appear inside the div
index.html
<form action="/" method="POST">
<input id='inputCity' name="inputCity" type="text" placeholder="Type", autocomplete="off">
<button type="submit">Send</button>
</form>
<div> {{ temperature }}</div>
main.py
from flask import Flask, render_template, request, jsonify
from weather_api import get_weather
app = Flask(__name__)
@app.route('/')
def weather():
return render_template('index.html', temperature=city_temperature)
def input_data():
city = request.form['inputCity']
city_temperature = get_weather(city)
if __name__ == "__main__":
app.run(debug=True)
I already tried changing to GET and I already tried creating another route
José Luiz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
I see you’re new to coding. Every function you define must be executed at some point if you want to use it later. In your example, you didn’t execute the function that returns the temperature, so it wasn’t used.
Fixed code:
from flask import Flask, render_template, request, jsonify
from weather_api import get_weather
app = Flask(__name__)
@app.route('/')
def weather():
city_temperature = input_data() # Execute the function and assign returned data to the variable.
return render_template('index.html', temperature=city_temperature)
def input_data():
city = request.form['inputCity']
city_temperature = get_weather(city)
return city_temperature
if __name__ == "__main__":
app.run(debug=True)
Now your code should work fine.