Components of the Flask application

Now that the backend database and tables have been created and loaded with data and the relationships between the tables have been modeled and generated, it's time to write the scripts that will create the Flask application. These scripts will contain views, models, and forms that process web requests, query the database, and return an HTTP response.

The web application is called the Arena application, as it lists all of the NBA arenas stored in the arena table in a drop-down list, and allows the user to display the location on a map along with a popup that contains information about the arena from spatial queries and table relationships.

The MVC method of web development allows for separation between the necessary components of a web application. These components include database models (the SQLAlchemy models described earlier), web forms for accepting application input, and a controller object that routes requests. The separation of components is reflected in the separate scripts. Making each component independent makes it easier to adjust without affecting other components of the application.

The database model will be contained in a script called models.py, along with the required module imports. The web forms (Python classes that create web page components such as drop-down lists and entry fields) will be contained in a script called forms.py. All of the views, which include the URL endpoints and the processing of the web requests to those URLs, will be contained within a script called views.py

A controller is an object generated from the Flask class and assigned to the variable called app. Each URL endpoint of the web application is defined using app.route and has an associated Python function (the view) that contains the logic to process the web request and return an HTTP response. The controller is used to route web requests to the correct URL endpoint and can distinguish between GET and POST HTTP requests. It is created in the views.py script. 

HTML templates are used to present the processed results of a web request. Using the Jinja2 templating system, the data contained within the web forms will be passed to HTML templates and sent back to the requesting web browser as a complete web page. The template for this application contains links to JavaScript libraries, including Leaflet, which allows the web page to present a map within the web page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset