Python introduction | Let's add SSL |
Running Flask from Python
So far, we’ve been running flask from the command line. It works well, but what if you want to run your python program and have it start flask? It’s not as hard as it might seem.
Let’s create a new python file using Visual Studio Code. It can be saved anywhere on your system.
Remember a python file name ends with .py
Before we can do anything else, we need to import the flask module:
from flask import Flask
The first step is to create a flask instance. You can do this by adding the following code. This creates a flask instance called flaskApp.
flaskApp = Flask(__name__)
This is the most basic way to define it, we’ll delve into a bit more info about what else we can include here later.
Creating the instance isn’t enough. We need to run flask. To do so, we add code that runs our flask instance.
if __name__ == '__main__':
flaskApp.run()
Now if you run the program and go to http://localhost:5000/, you’ll have a running flask server!
The only problem is, we just got faced with a file not found message. Let’s add something for the program to output.
Let’s add some pages
In the first part of the tutorial where we explored running flask from the command line, we created “routes” that told the program if we got an input where we could point it. Let’s see how we can use that here.
@flaskApp.route('/')
@flaskApp.route('/index')
def index():
return "Hello, world!"
You can get more info about what all of this does here, and learn about more that we can do with the routes functions.
What if we don’t want to have to add the routes manually, and instead have it automatically serve html files from a folder?
All that needs done, is to modify the instance of flask that we created earlier.
flaskApp = Flask(__name__)
Should become:
app = Flask(__name__, #same as our original flask instance creation
static_url_path='', #tells the server that requests at this address should be used to serach for static files
static_folder='/') #this tells the program to get the html files from the local folder. Setting it to something else will set it to a different folder
Now let’s create an html file in the same folder as our python file. We’ll call it index.html and place the following code in it.
<html>
<head>
<title>Welcome</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>This file is a sample</p>
</body>
</html>
Now if we run the program and go http://localhost:5000/ we’ll still see the “Hello, World!” we set up earlier using the routes function. If we open http://localhost:500/index.html though, we’ll be shown the html file we just created. This will work for any number of files, so if you create a new html file in the local folder, it will also be accessible using flask.
You’ll notice that we must include the file name extension (.html), or we get a 404 error. We can use routes to fix this. Let’s change the routes so that the index.html file will be served even without the file extension.
To do this we’ll add another import statement to the beginning.
from flask import send_from_directory
Then we’ll modify the routes statement.
@flaskApp.route('/')
@flaskApp.route('/index')
def index():
return send_from_directory("./", "index.html") #this is the only line that needs changed
Now if we go to http://localhost:5000/ we’ll see the index.html file served. It will also be served at http://localhost:5000/index and http://localhost:5000/index.html.
Python introduction | Return to top | Let's add SSL |