Open In App

Flask – Variable Rule

Last Updated : 05 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss Flask-Variable Rule using Python. Flask is a microweb framework written in Python it is classified as a micro framework because it doesn’t require particular tools or libraries, and it has no database abstraction layer form validation or any other components where pre-existing third-party libraries provide common functions it is designed to make getting started quick and easy with the ability to scale up complex application. 

Flask -Variable Rules:

  • By using variable rules we can create a dynamic URL by adding variable parts, to the rule parameter.
  • We can define variable rule by <variable-name> using this syntax in our code.
  • Variable is always passed as a keyword argument to the function with which the rule is properly associated.

Dynamic URLs Variable In Flask

The following converters are available in Flask-Variable Rules:

  • String – It accepts any text without a slash(the default).
  • int – accepts only integers. ex =23 
  • float – like int but for floating point values ex. = 23.9
  • path – like the default but also accepts slashes.
  • any – matches one of the items provided.
  • UUID = accepts UUID strings.

Simple flask program

In this code, we import the first Flask and then we initialize the Flask function and then we return the simple welcome line we need to run the Flask run command in the terminal to execute our code.

Python3




# first we import flask
from flask import Flask
 
# Initialize flask function
app = Flask(__name__)
 
 
@app.route('/')
def msg():
    return "Welcome To The GreeksForGreeks"
 
 # we run code in debug mode
 app.run(debug=True)


Output:

Flask - Variable Rule

 

String Variable in Flask

In this code, we import the first Flask and then we initialize the Flask function after that we made a string function string is already defined in the code we don’t need to specify the string and then we execute our code by a run in the terminal Flask run. and in URL we need to write 127.0.0.1:5000/vstring/<name> to run our vstring function.

Python3




# First we import flask
from flask import Flask
 
# initialize flask
app = Flask(__name__)
 
# Display first simple welcome msg
@app.route('/')
def msg():
    return "Welcome"
 
# We defined string  function
@app.route('/vstring/<name>')
def string(name):
    return "My Name is %s" % name
 
# we run app debugging mode
app.run(debug=True)


Output :

Flask - Variable Rule

 

Integer Variable in Flask

In this code, we import the first Flask, and then we initialize the Flask function after that we made an int function, int is not defined in a Flask so we need to first define it then we return the int function and run Flask run in terminal and for int page, we need to write function name in URL and also the integer value.

Python3




# first we import flask
from flask import Flask
 
# Initialize flask function
app = Flask(__name__)
 
@app.route('/')
def msg():
    return "Welcome"
 
# define int function
@app.route('/vint/<int:age>')
def vint(age):
    return "I am %d years old " % age
 
# we run our code in debug mode
app.run(debug=True)


Output:

Flask - Variable Rule

 

Float Variable in Flask

In this code, we import the first Flask and then we initialize the Flask function after that we made a float function float is not defined in a Flask so we need to first define it then return the float function and run the Flask run in the terminal and for float page, we need to write function name in URL and also the floating value.

Python3




# first we import flask
from flask import Flask
 
# Initialize flask function
app = Flask(__name__)
 
@app.route('/')
def msg():
    return "Welcome"
 
# define float function
@app.route('/vfloat/<float:balance>')
def vfloat(balance):
    return "My Account Balance %f" % balance
 
 # we run our code in debugging mode
app.run(debug=True)


Output:

Flask - Variable Rule

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads