Where are you going to do your coding? If you try to run your app, what other software is required? How are you going to save or publish your work? Configuring your IDE, dependencies, and VCS is essentially your workspace.
I can set up a Python web development environment.
I can create and view a blank Flask app.
Before we get into the structure and mechanics of our code, let’s build the most basic of Flask apps. It’ll help set some context as we then start to learn particular skills. It’s important that you start from scratch as it builds confidence knowing you can start from the ground-up.
One of the hardest things in all of programming is setting up your development environment.
Properly configuring a development environment can be a real pain. Rather than installing your language interpreters and dependencies, you can use a website to remote control a server preconfigured with most of the tools you need.
pwd -show the location where you are in the terminal
cd -is used to navigate between directories or folders
mkdir -is used to make a new directory
ls -list structure, show the list of items you have
clear -clears the screen
mv “name of item” -moves items around the directory
cat - catenate, lets you read files
touch “name of item”- create a new, empty file
Managing dependencies will be a constant challenge in programming. Over and over again, the work you’re doing will need to access other files. Keeping the relationships organized and making sure the necessary connections are always functioning is work.
Before you do anything, update your installer tool:
sudo apt-get update
Get the latest version of python that
apt-get easily allows, (manually installing the very latest version never seems worth it). This is likely to change a lot:
sudo apt-get install python3.6 python3.6-venv python3.6-doc binfmt-support
Tell our new Python to create a venv:
python3.6 -m venv /home/ubuntu/workspace/venv
Enable venv so we install and check for our libraries in this folder:
Make sure your python package installer is update:
pip install --upgrade pip
Tell python’s package manager to install the Flask library:
pip install flask
Create the file if it’s not already there:
In order for our
flask run to know which file to use, we specify with
.env files to save our environmental variables so we don't have to export them. You can totally use
.env files instead of Flaskinni's use of
Right click in the flask_init folder and select create new file called
hello_flask.py. If it’s already created, just double click to open the file
Type this into the editor:
hello.pyimport os # connects the OS library so we can lookup our own IP addressfrom flask import Flask # this connects our framework of tools we'll needapp = Flask(__name__) # here we create our app in memory. It's maybe the most important line of firstname.lastname@example.org('/') # if anyone tries to go to the homepage, their request will be routed to the method belowdef index():return 'Hello from Flask'if __name__ == '__main__':host = os.getenv('IP', '0.0.0.0')port = int(os.getenv('PORT', 5000))app.debug = Trueapp.run (host=host, port=port)
If you can understand some of the elements that are missing from this simple example, you’ll have a greater appreciation for the core of our Flask app and the many components that we’re about to study.
Our initial application, hello flask, had one singular route
@app.route('/') which is very basic. Just a homepage, that’s it. On the other side, “Flaskinni” includes numerous routes so you see blog and account pages just to name a few. Each route in Flaskinni calls templates with method calls like
render_template('index.html'). These are just as their name suggests, templates or outlines of the final HTML page a user sees. Templates are a big deal (⅓ of our whole MVT structure).
Want to know the difference between ‘GET’ and ‘POST’? You see them mentioned as a kwarg on line 16 in the picture below? Well, you should know the name Tim Berners Lee. GET and POST are two of the HTTP requests that travel over our network and provide us the Internet. Simply put, GET is a request for a page and POST is when the user is submitting a form. Knowing the types of HTTP requests gets really important if you ever decide to use Flask-RESTful to use this web app to power mobile apps, too.
In the Hello Flask application, there is no database or model schema being used. There are no objects like user, admin, blog post in the app. Also in the Hello Flask app, we had no database, but in Flaskinni there is a database, and a schema that shows how the database will be set up. Also in Flaskinni there is code that creates database tables and is able to send the data to the database in the format laid out by the schema.
Our initial Hello Flask application had no interactivity whatsoever as illustrated by the one simple message that is sent to the user. Our app only responds to GET requests, never a POST request. In other words, the user is only getting static information and is never sending information back to the server or requesting more specific information. For example in the Flaskinni application a user can post a blog post or can request or a certain blog post.
Libraries are expansion packs, they're files containing compiled code that allow the app to reference them later in other files. Libraries allow the app the run faster and have more capabilities. Instead of calling methods over and over while writing the app, the user can call the library file. The linking of libraries allows a file to gain the capabilities of the library module without having to define those methods again.