The Uses Of Flask In 2022.

The Uses Of Flask In 2022.

Big boss moments of using flask.

From what I've seen, entry-level developers are attracted to front-end development as it gives them a sense of seeing the progress that they can show their friends & family. Back-end development is not seen as attractive to a beginner, that's how I felt at least. I mean who wants to see the flour, eggs, milk & sugar you use for your cup-cake restaurant, customers want to see the fully fresh-baked cupcake ready to eat. Even though that's the case but what happens at the back is very important. A regular site has separate sections, we have the client side which is the front, and the back which consists of servers and a database. Let's dive deeper to the back where we'll meet the big boss flask.

giphy.gif Flask forcing me to put all the styling on "static" folder

Flask is a web development framework written in Python, I found it simple to use as It does not require libraries to function. Flask uses Jinja templates engine and Werkzeug toolkit. To be fair, I found it easy to install, It might differ on the IDE you're using but I use Pycharm. I took my time to read the documentation, this is where the fun begins.

Remember when you wrote your first code and saw "Hello World" printing on the console. It is that exciting when you launched your first local server using Flask. There are things you need to know before launching your first server. Since Flask is written in Python, you need to have a solid foundation in Python. One may ask how much Python knowledge one needs to work with Flask. Well, you need to go as far as understanding Object-Oriented Programming, you need to understand args, kwargs, and python decorators. You also need to know the basics of HTML, CSS, and the command line.

Here is how you print your Hello World with Flask:

from flask import Flask      # Imports flask
app = Flask(__name__)         # Setting up the app

@app.route('/')              # Sets the route for the page
def hello_world():            
    return 'Hello, World!'       # Returns 'Hello, World'

What I noticed from reading the documentation is that you need to have two folders that are very important when using Flask. You need to create a "templates" directory and a "static" directory. This is where the golden rule of separation comes in, your index.hml file must be inside the templates directory and your styles.css, favicon, and photos must be inside the static directory. Now you have built your server and your site is now on your local server. It is very important to re-edit the file paths inside Index.html since everything would be stored on static for styling and HTML file under templates. This is a must in order for this to work, you can refer to the documentation.

Flask made it simple to create your own server and run your site. Now your site has changed from having a front-end to having both front-end and back-end which is a server made using Flask. I don't know if you've noticed, but there are sites with outdated footers. It is very important to update your copyright dates, If you go inspect most of the website footers you'll see that they are hardcoded. We are programmers theirs a way to automate this using Flask so that you won't come back every year to change your footer.

from flask import Flask, render_template
import random
import datetime 

app = Flask(__name__)

@app.route('/')
def home():
       random_number = random.randit(1, 10)
       current_year = datetime.datetime.now().year
        return render_template("index.html",  num=random_number, year=current_year)



if __name__ == "__main__":
       app.run(debug=true)

Let's talk about the "render_templates" above, this simple means, with Flask and Jinja you can render templates like HTML or static files as mentioned earlier. It is also easy to work with APIs, see the example below.

    name = requests.get(f'your API link {name}').json()
    gender = requests.get(f'your API link {name}').json()

There are many things I learned myself using Flask and Jinja, here are a few tips you should not miss when using this framework, you can write multi statements in instances of using the "for loops", "if statements", etc. Jinja documentation shows the required

{% endfor %}

at the end of your multi statements, you must use '{%' at the beginning of the "for loop" and at the end. here is what it looks like inside your HTML file.

{% for num in all_numbers: %}
    <div class="content">
        <div class="card ">
            <h2>{{ num.title }}</h2>
            <p>{{ num.calculations}}</p>
            <a href="{{ url_for('show_num', index=num.id) }}">Read</a>
        </div>

    </div>
    {% endfor %}

There are many interesting projects that can be made using Falsk, I found it to be one of the most useful frameworks I've worked with, there is so much more that can come out from Flask which will follow in the next upcoming series of articles. This includes POST requests, RESTful and validating web forms with Flask-WTF. The back-end development is really interesting to work in with these tools and advanced features that can be added to websites and create interesting web applications. Flask applications can be also be easily hosted in many free hosting services.