4
\$\begingroup\$

I've made a Python Flask app and I have some questions about the structure of the main module (equivalent of index.py).

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, home, contact, and a dynamically created weather_forecast url.

Basically, when the home() function runs and renders the home page template, it runs this line: sites=db.create_site_list_sqlite() The 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 (climbing_area, city_id, lat, lon, and 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:

http://localhost:5000/weather_forecast/Ibex/5538080/38.881/-113.461/Delta

Thank you for any input.

\$\endgroup\$
  • \$\begingroup\$ Please show your template code. \$\endgroup\$ – Reinderien Jul 5 at 13:31
3
\$\begingroup\$

URL

URL components should generally appear from "broadest to narrowest" left-to-right. I'm not totally convinced that this is currently the case - the weather site is probably "broader" than the latitude and longitude.

Is the city within a climbing area, or vice versa? If a climbing area is within (or smaller than) a city, their positions should be reversed.

How many of those parameters can be inferred from other parameters? If you only specify the city, could default coordinates be inferred? Try to omit as many optional parameters as possible from the path, and move them to query parameters.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.