Our first web page

The first step is to install flask. As usual, we can install flask using pip inside a virtual environment.

Using the command line, navigate to the folder where you have been writing your chat application and enter the following commands:

python3 -m venv env

source env/bin/activate

pip install flask

Pip should take care of installing flask, along with its dependencies, inside your new virtual environment.

Once this has finished, create a folder named server alongside your Python files for the previous chapter. This is where we will be placing our web service for the chat application.

Inside this folder, create a file named server.py and place the following code inside:

from flask import Flask

app
= Flask(__name__)
app.config.from_object(__name__)

@app.route("/")
def index():
return "This is a flask website!"


if __name__ == '__main__':
app.run(debug=True)

With these few lines of code, we now have a functioning web server. We just need to run it before we can visit it in a web browser.

If you have closed your terminal session from earlier, open a new one and navigate into your root directory for this project. Load the virtual environment with source env/bin/activate, move into the server folder, and run python3 server.py. Your terminal should respond with something like this:

* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 143-718-128

Head on over to http://127.0.0.1:5000/ in a web browser and see your web page in action:

You should be able to see the string that was returned in your index function appear in your web browser. So, how did this happen? Let's break down the code sample:

from flask import Flask

app
= Flask(__name__)
app.config.from_object(__name__)

The first three lines are very standard flask setup. You will likely see every flask application include these three.

In order to use flask, we obviously need to import it. The main class which handles a flask application is called Flask.

It is convention for your instance of the Flask class to be named app.  The next two lines simply handle creating the flask instance and configuring it.

After our application is configured, we need to assign some URLs to it. We do this using decorators:

@app.route("/")
def index():
return "This is a flask website!"

One particular decorator we can use is the @app.route decorator. This adds a route to your application using the specified string as the path.

In this example, we used the string /, which means the root of your URL.

To experiment with this, try changing this parameter to /hello, then visiting http://127.0.0.1:5000/. Since there is no longer a route at your root URL, this will result in a 404 error. If you instead visit  http://127.0.0.1:5000/hello, you will once again see your message appear.

Once all of our routes are configured, we just need to run the application. Luckily, flask provides an easy way to achieve that:

if __name__ == '__main__':
app.run(debug=True)

We simply need to call the run method on our app to get our server running. We pass debug=True while developing, as this gives us a very detailed stack trace if any errors occur.

Now we know how to return a simple web page from a flask application. However, we won't be dealing with web pages in our chat application. Instead, we just want raw data.

In order to receive this data from a web service, we can return something called JavaScript Object Notation (JSON). It provides a nice way of sending data over the internet, and is very similar to a Python dictionary. So similar, in fact, that JSON data is written as a dictionary and read back into one too.

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

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