F-Flask? app route? app.run? What.. What you need 2 know about flask

Flask is a popular web framework for Python that can be used to build web applications.

If you’re running the project on Replit, you can skip the install step, it’s a default install on Replit python repls.

To use Flask, you must first install it. You can install Flask by running the following command in your terminal:

pip install flask

Once Flask is installed, you can follow these steps to create a basic Flask application:

  1. Import the Flask module and create a Flask web server object:
from flask import Flask

app = Flask(__name__)
  1. Define a function that will run when a client requests a specific URL using the route() decorator. For example, to create a route for the home page, add the following code to your Python file:
@app.route('/')
def home():
    return 'Hello, World!'
  1. Finally, run the Flask application with the following code:
if __name__ == '__main__':
   app.run()

This code snippet creates a web server that listens to incoming requests on port 5000.

You can access your home page (if locally hosted) by navigating to http://localhost:5000/ in your web browser.

If your project is hosted on replit, access it from the URL that looks like one of these:

  • https://projectname.username.repl.co
  • https://projectname--username.repl.co

This is just a basic introduction to Flask. Flask provides many more features, such as the ability to render HTML templates and handle form data.

Let’s continue with some more advanced features of Flask.

  1. To render an HTML template, you can use the render_template function from Flask. First, you need to create a templates folder in your project directory, and then create an HTML file inside that folder. For example, if you create an HTML file called index.html inside the templates folder, you can render it with the following code:
from flask import render_template

@app.route('/')
def home():
    return render_template('index.html')
  1. To handle form data, you can use the request object from Flask. Here’s an example of how to handle the form data from a POST request:
from flask import request # go learn more about requests here: https://ask.replit.com/t/what-you-need-to-know-about-the-requests-module/36625?u=idkwhttph

@app.route('/submit', methods=['POST'])
def submit():
    name = request.form['name']
    email = request.form['email']
    # do something with name and email
  1. Flask lets you easily add arguments to a url. For example, an argument would be https://example.com/?argumentName=value. You can also pass multiple arguments to a url by doing https://example.com/?argumentOne=valueOne&argumentTwo=valueTwo. To retrieve these arguments in your Python code, you may do:
from flask import request

# rest of the code for your app
@app.route('/args')
def args():
    arguments=request.args
    return [x for x in request.args.values()]
  1. Flask provides a convenient way to redirect the user to a different URL using the redirect function. Here’s an example of how to redirect the user to the home page:
from flask import redirect, url_for

@app.route('/redirect')
def redirect_example():
    return redirect(url_for('home'))
  1. Flask also lets you create app routes on demand. For example:
# rest of the code for your app
@app.route('/<param1>')
def example(param1):
    animals={
        "cat": {
            "name": "Bob"
        }
    }
    return animals[param1]

These are just a few examples of the many features that Flask provides.

Flask has extensive documentation available online, which can be found at the following link: Welcome to Flask — Flask Documentation (2.0.x)

This documentation includes a Quickstart guide for beginners, a detailed User’s Guide, and a set of API reference documents for each module and class in Flask. The documentation also includes examples and tutorials to help you get up to speed with Flask quickly.

In addition to the Flask documentation, you may also find the official Python documentation and the Werkzeug documentation helpful, as Flask is built on top of these libraries.

4 Likes

On replit it’s installed by default, so this is unnecessary.

Remember to use indentation please.

Not on replit. Access it from the URL that looks like one of these:

  • https://projectname.username.repl.co
  • https://projectname--username.repl.co

Please use indentation, because if you don’t, the code won’t run.

3 Likes

I know a little more about Flask, can you make it a wiki so I can edit it?

(ask a mod to make it a wiki)

1 Like

Add templating and render_template that’s the most important thing when getting started with flask, especially if you haven’t used a template engine or jinja before.

Also you should add a second about flask_socketio cause socketio

4 Likes

Flask’s request module and the requests module are two different things; it looks like you have them confused as the same or similar thing.

Flask’s request module handles certain data and information about incoming requests to your webserver.

The requests module on the other hand, handles and allows your code to send requests to a webserver, not handle incoming ones.

1 Like

SocketIO is unnecessary for beginners because it requires more than backend work and is considered to be more of an advanced thing to implement in Flask.

1 Like

@TheCosmoKing don’t necropost (its an inconvenience for mods/staff to deal with) this topic is dead (nobody has replied in a while).

2 Likes

I didn’t realize it was that old, I just got it in a Replit newsletter. Sorry about that.

2 Likes

For beginner beginners yes, but it’s good to learn about once you have the super basic stuff down so you don’t pull your hair out trying to do something that socketio makes super easy (I know from experience)…
also the mods cannot legally stop us necroposters…

3 Likes

I wouldn’t bet on that :wink: but as of right now we aren’t really against necroposting

4 Likes