Line 11: Change the return so that now it returns render_template("about.html"). Line 10: Change the function so it is now def about(): Let’s make a change similar to what we did before to our main.py. LinkedIn, and the community web page for Flask itself. Applications that use the Flask framework include Pinterest, Flask is a micro web framework written in Python. Let’s create an about.html inside the templates folder. Now visit your localhost and see the changes: Let’s add more pages Line 7: We change the return so that now it returns render_template(“home.html”). Learn more about render_templates() function. Then it will render the template for which you ask. render_template() looks for a template (HTML file) in the templates folder. Line 1: We imported render_template() method from the flask framework. Now we need to change our main.py so that we can view the HTML file we created. Remember to always keep the main.py outside of your templates folder You need to create a templates folder and put all your HTML files in there. The Flask Framework looks for HTML files in a folder called templates. HTML, CSS, and Virtual Environments HTML and Templates in Flaskįirst create a new HTML file. Let’s make our website look nicer by adding HTML and CSS. Now run the main.py again and go to So far we have been returning text. we added a new route, this time to /salvador. Let’s add more routes so you can see the difference. Go to that address and you should see the following: Congrats! You made a website with Flask! More Fun with FlaskĮarlier you saw what happened when we ran main.py with one route which was app.route(“/”). If you do not know the meaning of this (like I didn’t when I started - this article is really helpful), the main idea is that 127.0.0.1 and localhost refer to this local computer. The important part is where it says Running on. In your terminal or command prompt you should see this output. In your Terminal or Command Prompt go to the folder that contains your main.py. Having debug=True allows possible Python errors to appear on the web page. When we run main.py, it will change its name to _main_ and only then will that if statement activate. If we import another script, the if statement will prevent other scripts from running. Line 9: When you run your Python script, Python assigns the name “_main_” to the script when executed. Line 6–7: When the user goes to my website and they go to the default page (nothing after the slash), then the function below will get activated. Then this will be the default page of Google. For example, if I go to a website such as “/” with nothing after the slash. Here we are creating a new web application. We are creating an instance of the Flask class and calling it app. This current file will represent my web application. Line 1: Here we are importing the Flask module and creating a Flask web server from the Flask module.
This piece of code is stored in our main.py. The code lets us run a basic web application that we can serve, as if it were a website.
Flask lets us focus on what the users are requesting and what sort of response to give back. It makes the process of designing a web application simpler. What is Flask? Flask (A Python Microframework) It will also figure out what response to send to the user. It will figure out what those requests are dealing with and what they are asking. We will write code that will take care of the server side processing. Then it displays what you asked for on a page in the browser. The Google Server sends back an HTTP response that contains the information that my web browser receives.
The Google Server receives the request and needs to figure how to interpret that request. What happens is that an HTTP request has been sent to a server.įor example, when I go to my address bar and type, then hit enter, an HTTP request is sent to a Google Server. When you type the name of a website in the address bar of your browser and you hit enter. Let me give you an example of how you use it everyday. The internet uses it to interact and communicate with computers and servers. What is HTTP and What Does it Have to do with Flask? All the code used in the tutorial is available in this GitHub Repository. In each section, I will show pieces of code for you to follow along.