Sample Project
The provided Sushi Sample Project (zip file) is a simple database-driven web application.
The Sushi Sample Project is a bit simpler that what’s being asked of you, but everything you need is there. The code is in Python, and uses a SQLite dabase for persistance. The code is structured in an MVC style to make development easier.
Flask
Flask is a liteweight web application framework for Python.
I highly recommend you complete the Flask “Quickstart” tutorial in order to understand the provided sample code. You should read and understand everything in “Quickstart” up thru the end of the section titled “Accessing Request Data”. (That’s all you need to understand the Sushi Sample Project code and complete this project.)
To run the Sushi Sample Project, simply run flask_app.py, either in IDLE or from the command line. Then open your browser and visit the URL shown in the output. (Probably http://127.0.0.1:5000/)
A simple Flask app
If you want to try the simplest possible Flask app, create a new file with the following.
# Filename: hello.py
from flask import Flask
app = Flask(__name__)
@app.route('/') # The "path" part of the URL
def hello_world(): # The Python that runs when you visit "/"
return 'Hello, World!' # The page text that's returned when you visit "/"
if __name__ == '__main__': # If running directly from IDLE, launch now
app.run()
Jinja
Jinja is a template language, designed for use with Flask. (It works with other frameworks too.) Jinja is similar to other templating languages.
What is a template? Consider a typical web site. Many parts of any given page are repeated on all pages. The nav bar and footer are examples of two parts of a page that are likely the same across the entire site. Instead of copy/paste-ing changes into every HTML page (that’s the boring, time-consuming, and erorr-prone way to do it), it’s best to write those sections once, and include them on each page. That’s the primary benefit of a template langauge.
The Jinja documenation has waaaay more than you need for this project. Once Jinja is installed and working (if the sample code works, than you’re good), all you need is the first few sections of “Template Designer Documentation” (“Synopsis”, “Variables”, and maybe “Filters”). You may also want the documenation section “List of Control Structures” to see how to turn data into tables, but you can simply re-use the patterns shown in the provided Sushi Sample Project.
If Jijna is not installed, read its documentation for instructions!
Using Jinja
Jinja templates are organized into blocks. A HTML page is made up of blocks. You might have a block for your title bar, a block for your footer, and a different block for the body of your page.
In this example, the full page template is found in base.html. Any other page can use the pattern built in base.html by importing it.
{% extends 'base.html' %}
{% block title %}Sushi Menu{% endblock %}
{% block content %}
<p>Your page content goes here.</p>
{% endblock %}
The template snippet above shows how most of the structure of the page comes from the file base.html. Only the “title” and “content” blocks of the page are changed.
Jinja brackets
Brackets {} have a special meaning in Jinja.
```django {{ }} Inserts a variable value from the model
{% %} Control structures, e.g. if or loop
{# #} Comment; not processed ```
SQLite
You should use SQLite as your database engine for this project. (If you do, the only line you need to change in the database sample code is the path to your database file.) SQLite is unique in that the entire database encapsulated as a regular file. This means to share a database, you simply share a copy of the database file. It’s easy to install, easy to use, and works well with Python.
The documentation on the SQLite page is a little bit heavy, and you won’t need most of it. Go straight to the section on “SQL Syntax”. You’ll only need the basic SQL commands you learned during Tech Bootcamp: CREATE TABLE, SELECT, INSERT, UPDATE, and DELETE.
One thing that’s different about SQLite as compared with other database engines is that it does not enforce data types. Thus, the CREATE TABLE syntax may be a slightly different than you’re used to. Each database provider typically has some minor differences in data types, so you may find the documentation useful.