Assignment 0

This assignment is to write a basic Flask application to show that you can do so, and to work out the kinks of creating and submitting applications.

Attention

This is a solo assignment, so you must complete and submit it on your own. You may talk about general concepts with your classmates, but all code you submit must be your own or adapted from the class examples.

Note

The bulk of this assignment description is about setting up your first project and packaging it for submission. Since this process will be the same for the remainder of the class, you may wish to refer back to this assignment in preparing future ones.

Note

I have created a video to help you set up the project. The link is under resources.

Before you start the project, you will need to to have completed the local setup and have a Python virtual environment with Flask and Flask-Script installed.

Getting Started

To get started:

  1. Start PyCharm.
  2. Create a new project.
  3. Select ‘Flask’ as the project type.
  4. Pick a location and directory for your project.
  5. If necessary, select a ‘Project interpreter’ that is your virtual environment.
  6. Accept the default template options (Jinja2 and the templates folder).

Activating Version Control

Once your project is created, activate Git for it:

  1. Select VCS -> Import into Version Control -> Create Git Repository….
  2. Select your project folder and confirm the dialog.
  3. Select your main Python file (e.g. cs3320-a0.py) and select VCS -> Git -> Add to register the file with Git.
  4. Select VCS -> Commit Changes, enter a commit message, and click Commit.

Note

If you are on Windows and PyCharm complains that it cannot find Git, follow the setup instructions and tell it where your Git installation is located.

Your Git experience will also be better if you download the gitignore template and save it in your project as a file called .gitignore (note the initial ‘.’ and the lack of a file extension — both of these are important).

Setting up the Management Script

Your initial application consists of a single Python file and two empty directories, templates and static.

All of our applications will use a manage.py file with Flask-Script to control them. This allows us to have a consistent interface to run your applications. For now, you don't need to understand how manage.py works; just do the following:

  1. Download the manage.py template and save it into your project.
  2. Add it to Git (VCS -> Git -> Add)
  3. If your main Python file has a hyphen in its name, rename it (select it and select Refactor -> Rename…) so that its name consists only of letters, numbers, and the underscore (‘_’), with the .py extension.
  4. Edit manage.py to change the import line (indicated with a comment) to import from your main file. For example, if your file is called A0.py, this line should read
    from A0 import app
    

At this point you can run your application. If you Run the manage.py script, it will produce some help output. Then go to Run -> Edit Configurations…, choose ‘manage’, and in the Script parameters add the following:

runserver --debug --reload

Rename the run configuration to ‘Run Server’ (to distinguish it from other actions you will run), and click OK.

You can now run it again, and your server will run! You can browse to http://127.0.0.1:5000 to view it.

Making Real Changes

All of that is setup. Now, you need to make a few changes to write a real application that isn't just copied and pasted:

  1. Create a template, in templates, called index.html. You can select the templates folder and choose File -> New to do this.
  2. In your template, create a basic HTML skeleton that has a title of ‘Assignment 0’ an initial h1 heading that says ‘Assignment 0’, and then has 5 paragraphs of lorem ipsum. You can generate Lorem Ipsum with a template directive:

    {{ lipsum(5) }}
    
  3. Modify your main application file's entry point (called hello_world and routed to / in the default PyCharm template) to render your template (with flask.render_template) instead of returning a bare string.

To complete this last step, you will need to either import render_template from the flask module, or add an import flask line to access it as flask.render_template.

Preparing your Submission

The manage.py script contains a package command that will prepare a script for submission. Your submission must be created with manage.py package.

To use it from PyCharm, go to Run -> Edit Configurations… and select your ‘Run Server’ configuration. Click Copy Configuration (the icon with two pieces of paper in the toolbar above the configuration list), and rename your configuration Prepare Package.

Modify its Script parameters to be the following:

package -o a0-submit.zip

When you run this configuration, it will create a file called a0-submit.zip that contains all your code.

Note

Make sure that all your code is committed to Git before running the package script! It only packages Git-commited code.

You can also run the package script from the terminal, as:

python3 manage.py package -o a0-submit.zip

You can test your package by unzipping it somewhere else, and trying to run

python3 manage.py runserver

on the unpacked code.

Note

On Windows, you may need to use python instead of python3.

Submitting the Assignment

Submit the resulting zip file to TRACS.

This packaging and submission procedure will be used for all of your assignments.