Serving pages using a web server: Flask

Reminder

If you have doubts about HTML, CSS, JS and/or the client-server architecture, please review the previous lesson

At this point you should be able to create HTML pages, stylize them using CSS and run code in the browser with JavaScript. Clients send requests to the servers, the servers processes the requests and send responses to the clients, that display them. The web servers usually respond by sending HTML, CSS and Javascript. The response is then handled by the browser, that runs the JavaScript code, displays the HTML and uses the CSS to style the HTML. If you are loose when you read this paragraph, run to the previous lesson and review it!

Remember also the python programming learnt in the previous lessons. It will provide us with the basic knowledge to know what the Flask web server is doing. The source runs strong with a consolidated knowledge :)

In this lesson we will use repl.it, since it allows us to host a server in their hosting service, so we will be able to deploy our web pages using their hosting!

Serving HTML content. The source awakens

Take a look in the code from repl.it. Open it in a full screen, clicking in the top right corner (open in repl.it) button or navigating to https://repl.it/@AlbertoGil/flask-test. Check out the different files there:

The served content can be accesed clicking the popout button or navigating to https://flask-test.albertogil.repl.co.

Now navigate to the /goodbye on the end. What do you see? How do you think python generates what you see?

How to serve HTML content

There are different functions to serve content using Flask:

Flask functions

Creating our first formulary

Assignments

We want to deploy the amazing HTML web page that we created the previous week. No one wants to miss it :)

  1. Making our fake news worlwide accesible!
    • Create your own web application server copying the files from the repl.it provided and check that it works and it is accesible in the domain assigned by repl.it to your repl.it
    • Create different routes that returns different content. Y
    • Insert content into the HTML pages so instead of write the routes in the navigation bar of the browser, we can click into the links provided in your HTML page. HINT: Use <a> tags and its corresponding attributes.
    • Deploy a template using variables to return with different calls. The template should be called from two different routes named atleti and rayo. Does it display different content in the browser?
    • EXTRA. You can make any change to the repl.it apps modifying the .py, .css, .js or .html files! You will see if there is any error when the code is automatically deployed. As long as the website runs, all plays are allowed.
    • EXTRA. You can create a formulary to handle user data in your web server. Once you have seen an example of a formulary that collects a text input from the user in the attribute name, it sends it to the app server in the route getName and it returns a "hello <TEXT_CONTAINED_IN_NAME_ATTRIBUTE>", you can create more complex formularies. Use the source, Luke!