In Flask, a variable is created using tags inside route() function. To create a variable you just need to put < and end it with > , for eg. <name> this is a variable with identifies name. How to use a variable in Flask ? We can use a variable to receive data from URL (by making a part of URL dynamic). To understand this let me give you a simple example.
from flask import Flask app = Flask(__name__) @app.route('/hello/<name>') def hello_name(name): return 'Hello %s!' % name if __name__ == '__main__': app.run(debug = True)
In the above code, you can see in route() function, a <name> tag is used to create a variable. Which is receiving some data from the URL. In simple words, the content after /hello/ will be treated as data for the <name> variable.
So, when we open the URL as – http://localhost/hello/VikashMishra then it will return Hello VikashMishra as it is taking the part after /hello/ as a string.
In case if you want to specify the data type of the variable, then you can hard-code it into the variable tag itself. for eg. : <int:data>
DATA TYPES FOR VARIABLES
- string – %s is used to display data at its occurrence place. Used to store string
- int – %d is used to display data at its occurrence place. Used to store integers
- float – %f is used to display data at its occurrence place. Used to store decimal values.
- path – accepts slashes used as directory separator character.
Example of Python Code
from flask import Flask app = Flask(__name__) @app.route('/blog/<int:postID>') def show_blog(postID): return 'Blog Number %d' % postID @app.route('/rev/<float:revNo>') def revision(revNo): return 'Revision Number %f' % revNo if __name__ == '__main__': app.run()
Run the above code and visit URL http://localhost:5000/blog/11 in the browser.
The number is used as argument of show_blog() function. The browser will display the below output:
Blog Number 11
Now enter this URL in the browser − http://localhost:5000/rev/1.1
The revision() function will take up the floating point number as an argument, and produce the result shown below in the browser
Revision Number 1.100000
So now you can understand the data type and variable type in Flask. Before we end up this chapter, I’ll like to go little further and tell you a few things more. It’ll help you understanding the URL concept better.
The URL rules in Flask are based on Werkzeug’s routing module. This ensures that the URLs formed are unique and based on precedents laid down by Apache.
Here if you are typing /home and /home/, it might happen that you find it similar, but there is a big difference into it. if you enter /home then home will be treated as a value in most cases. But if you enter /home/ then it will be considered as a complete URL.
Let us try this with an example of python Code
from flask import Flask app = Flask(__name__) @app.route('/dashboard') def hello_flask(): return 'Hello Dashboard' @app.route('/home/') def hello_python(): return 'Hello Home' if __name__ == '__main__': app.run()
Both the rules appear to be similar, but in the second rule, trailing slash (/) is used, due to which it becomes a canonical URL. That is why, using /home or /home/ returns the same output. However, in case of the first rule, /dashboard/ URL results in 404 Not Found page.
Woohh,,,,,, completed the big part, now we can move on the interesting ones… stay tuned..