Routes

Routes allow us to add additional functionality to our application.

Our application can say hello world now, but we’d like it to do more than that. We can map different functionality within our application to different URIs using routes.

You might remember from the last section that to see our application, you visited http://localhost:3000/. This is the base URL of our application. localhost is just a special alias for the IP address for your computer. The 3000 refers to the port the application is running on - generally websites run on port 80 or 443, and the port number is not shown in URLs, but when we’re developing on our local machines, other ports are often used and we have to explictly include them.

We can make our application respond to any path beyond the localhost:3000, for example http://localhost:3000/hello, or http://localhost:3000/hello/world. We do this using routes. Currently, our application has only one route.

Here’s our single route:

@app.route('/')
def hello():
    return "Hello world!"

In Flask, you define a route using the @app.route('/') syntax. The function directly after this line (called hello() in our example) will be run when a request is made to this route.

Our route is listening at the root URI of the application which is expressed as “/”, and so when we visit “http://localhost:3000/” our application will run the hello() function. If we wanted to create a route that could be accessed by visiting “http://localhost:3000/coolroute”, we’d define the route with @app.route('/coolroute'). We would then define a new function right after that would be run when a request is made to “/coolroute”.

Let’s add another route where we will show the user an HTML page that includes a form for them to submit book data:

# Show the form to create new book
@app.route('/book/new')
def new_book():
    return render_template('new_book.html')

On the last line, you’ll notice instead of returning a string, we’re returning a call to the “render_template” method, and passing an HTML file. We’ve supplied this HTML file for you - you can find it in the “templates/” folder (Flask by default assumes any template HTML is stored in the “templates/” folder).

Below are the contents of “templates/new_book.html”. As you can see, it is mostly just HTML like you saw in the “Building Blocks of the Web” workshop, with a little bit of extra Python added in within either {{ }} or {% %} blocks. For now, don’t worry about what those blocks are doing, we’ll see it more in later templates and explain it. The important thing to see here is we’re defining an HTML form here to accept data, and we set the URL the form submits to as “/book”, which we’ll need to create within our application next. Also notice on that same line, the method we’re using is “post”. We’ll explore templates a bit more in a later section.

{% extends 'base.html' %}

{% block content %}
  <h1>Submit a new book</h1>

  <p>To submit a new book, enter the data for the book below, then press the "Submit" button.</p>

  <form action="/book" method="post">
    <label for="form-title">Title:</label>
    <input type="text" name="form-title" size="100" />
    <br /><br />
    <label for="form-cover-image">Cover Image URL:</label>
    <input type="text" name="form-cover-image" size="100" />
    <br /><br />
    <label for="form-description">Description:</label>
    <textarea name="form-description" rows="10" cols="50"></textarea>
    <br /><br />
    <label for="form-reason">Reason:</label>
    <textarea name="form-reason" rows="10" cols="50"></textarea>
    <br /><br />
    <input type="submit" value="Submit" />
  </form>
{% endblock %}

Now visit this URL to see the form: http://localhost:3000/book/new

For more information on Flask routes see: https://pythonbasics.org/flask-tutorial-routes/