Learn to Code via Tutorials on Repl.it!

← Back to all posts
Flask Tutorial!
ruiwenge2

This tutorial will cover a lot about Flask and its functions. But first of all...

What is Flask?

Well, Flask is a micro web framework written in Python. It allows you to build web applications.

This tutorial will teach you the following:

  • Routing a function
  • Returning HTML code
  • Templating
  • Handling GET/POST requests
  • Redirecting
  • Sending a file

Now we know what Flask is, let's create a Python repl, and get started!

To use Flask, we have to import the Flask class and create an instance. Then we'll route and run a function, and run the application:

After you run the code (and wait for the packaging), you will see a page showing a page not found error (because we haven't added any pages yet).

To add a page, we will use the route decorator, and create a function that will return some HTML to the page.

When you run the code, you will see a page that says hello world. To add a page in a different path name, you can use @app.route("/PATHNAME"), then create the function.

You can also have variables to route the function by using the less/greater than signs:

This will make it so that if you enter anything as the path name, it will return hello world. Remember to include the variable in the parameter! To use the variable in the function, we can simply just type the variable name:

Now this code will make it so if you enter /python as the path name, it will return python, and if you enter /replit as the path name, it will return replit.

To have static files (such as CSS or JavaScript), create a folder called static, and files will be kept in that folder. The path name for the static files will be /static/FILENAME.

Templating

Now this part will get a bit more interesting. So instead of returning a string in the function, we can render an HTML template. To do this, create a folder called templates, and create a file called index.html. We will have to import the render_template function from Flask:

The following code will render an HTML template:

In the index.html file we have:

When you run your code, you should see a button that says Click me!.

Variables

To use variables in your template, use curly brackets in the HTML code: {{VARIABLENAME}}. In the Python code, we will need to create the variable and inject it into the template. In the HTML we have:

And in the Python code we have:

You should see I like pizza when you run the code. You can add however many variables you would like, but be sure to add parameters containing the variables into the render_template function.

If/Else

Flask allows you to use if and else statements in your HTML template. To create if and else statements in the template, use this syntax:

So let's create an if/else statement. In the Python code we have:

In the index.html, we use the variable number in the if/else statement:

When you run this, you should see The number is 5..

For Loops

Flask also allows you to use for loops in HTML templates. This can save a lot of code. To create for loops, use this syntax like if/else:

Let's try it. In the Python code we have:

And in the HTML template we have:

The page should now show 0 1 2 3 4 5 6 7 8 9.

You can also use lists in a for loop. To do this, first create the list in the Python code:

In index.html we have:

You should get this when you run it:

Handling GET/POST requests

To recieve GET/POST requests, we will need to import request from Flask, and in the route decorator, we'll have to add the methods:

In index.html:

Once you enter a value in the input, you should see the text you entered. You can also change the form method to POST.

Redirecting

Redirecting is easy with Flask. Just import the redirect function and use it:

So if you wanted to redirect to the Replit website, you would do:

Sending Files

Sending files is also easy with Flask. You will need to import send_file from Flask:

So you would need to have a file called image.png. The output would show the image file. If you send files, you must have access to them.

That's the end of this tutorial, and I hope you liked it and found it helpful!

Voters
ruiwenge2
motraor3
MystPi
tyrx420
bendev9
palodog
CodingElf66
22yeetz22
khms-wHdwHd
Loshad
Comments
hotnewtop
codingjlu

Alright THIS IS A GOOD TUTORIAL THAT DESERVES an upvote (or two) :)

ruiwenge2
VulcanWM

Woah nice!

ruiwenge2

thanks @VulcanWM

VulcanWM

i just dont get how send files work @ruiwenge2

ruiwenge2

@VulcanWM for example you uploaded a file called file.png in the main directory. then you'll have to import send_file from Flask, and the file that you use will be file.png

see here:

VulcanWM

oh makes sense, thanks! @ruiwenge2

ruiwenge2
ruiwenge2

the cool thing is you can use the img tag to embed that image, like <img src="https://illfatedunfitpercent.ruiwenge2.repl.co">
@VulcanWM

VulcanWM
VulcanWM

thank you! this just helped me in something i was trying to do right now! @ruiwenge2

ruiwenge2

i'm just curious, what were you trying to do? @VulcanWM

ruiwenge2

oh! i think it was the pfp @VulcanWM

VulcanWM

Yep the way you actually went on the repl to see the code @ruiwenge2

ruiwenge2
Yoplayer1py

@ruiwenge2 What will send_file do ?

VulcanWM

It will send an image file or something to the person opening the website. An example is here: https://replit.com/@VulcanWM/OvalEyes
The output will be shown here: https://ovaleyes.vulcanwm.repl.co/pfps/vulcanwm @Yoplayer1py

VulcanWM

i see you're on the website lmfao @ruiwenge2

ruiwenge2

@VulcanWM OvalEyes is a creative name

VulcanWM

ty (i only made it that bc i have oval eyes) @ruiwenge2

ruiwenge2
CodingElf66

Really nicely written tutorial, I'm reading it right now and I have a clear view of Flask, thank you @ruiwenge2! Tysm! This surely needs more upvotes to match your hard work and everything! Here's an upvote!!!

ruiwenge2
Loshad

good

JWZ6

so cool :)

ruiwenge2

@JWZ6 thanks

JWZ6
ruiwenge2

@JWZ6 its my birthday today :)
and i made a multiplayer pong game. do you want to play against me?
https://replit.com/talk/share/MultiPong-A-multiplayer-pong-game-and-its-my-birthday-today-D/143432

JWZ6
ruiwenge2
A036Nikhil

can you please create a tutorial on Creating Login form using flask and replit database?

ruiwenge2
QuantumCodes

Wow! The best tutorial I've seen till now! Thanks for making this! For the get or post method, html gives data to py? Can we also send data from py to html?

ruiwenge2

@QuantumCodes you can receive data in html with a get request, but not post

QuantumCodes

@ruiwenge2

Gets from post??

ruiwenge2
JWZ6

pogger

ruiwenge2

thanks @JWZ6

JWZ6

@ruiwenge2 like srsly dude

ruiwenge2

@JWZ6 what?

JWZ6

@ruiwenge2 is good

ruiwenge2

@JWZ6 yay!

JWZ6

@ruiwenge2 dis deserves more updoots

ruiwenge2

well
its only been several hours
@JWZ6

JWZ6
JakeHu2020

Nice, I needed to see one of these so badly. Thanks!

ruiwenge2
CosmicBear

jinja described as "curly brackets and percent signs".

CosmicBear

Chit CHat

ruiwenge2

@CosmicBear i changed it though lol

CosmicBear

@ruiwenge2 It is a good description though

CosmicBear

of how to use it

ruiwenge2

@CosmicBear thanks!

CosmicBear

@ruiwenge2 i didn't know about send_file

ruiwenge2

@CosmicBear now you know!

EpicGamer007

wow, this is pretty nice. very concise, i learned a lot. :)

ruiwenge2