I've made a Python Flask app and I have some questions about the structure of the main module (equivalent of
Here is what it looks like:
from flask import Flask, render_template import database as db import weather_forecast as wf import os app = Flask(__name__) @app.route("/") @app.route("/home") def home(): return render_template('home.html', sites=db.create_site_list_sqlite(), api_key=os.environ.get('GOOGLE_MAPS_JS_API_KEY')) @app.route("/contact") def contact(): return render_template('contact.html') @app.route("/weather_forecast/<climbing_area>/<city_id>/<lat>/<lon>/<weather_site>") #creates url using climbing area name, city id, lat, lon, and location of weather site def weather_forecast(climbing_area, city_id, lat, lon, weather_site): return render_template('weather_forecast.html', climbing_area=climbing_area, city_id=city_id, daily_forecast=wf.format_daily_forecast(city_id), extended_forecast=wf.format_extended_forecast(city_id), historical_forecast=wf.get_historical_weather(lat, lon), dates=wf.get_date_range(), lat=lat, lon=lon, weather_site=weather_site, sites=db.create_site_list_sqlite(), api_key=os.environ.get('GOOGLE_MAPS_JS_API_KEY'), image_url=wf.image_choice(lat, lon, city_id)) if __name__ == '__main__': app.run(debug=True)
I have three pages in my application,
contact, and a dynamically created
Basically, when the
home() function runs and renders the home page template, it runs this line:
create_site_list_sqlite() function returns a bunch of data from an SQLite database, which is used to populate some
a href hyperlinks. When a user clicks on one of those links, it goes to a weather forecast page, created by the
def weather_forecast() route. That same data is then used to populate the URL in the proceeding weather forecast page.
My reason for doing this is, it was the best way I could figure out to run the
create_site_list_sqlite() function. I could have populated the
weather_forecast url by re-running that function but it seemed redundant.
Is this good practice to run the
create_site_list_sqlite() function (or any others) as few times as possible?
Also, because I needed several pieces of data from what that function returns (
weather_site), the url for a weather forecast page is rather long. I personally think its fine but is there some reason to tighten this up if possible, just as an example, here is what one of the weather forecast page URLs looks like:
Thank you for any input.