Flask-1 - Python With Naveen PDF
Flask-1 - Python With Naveen PDF
Framework
by
Naveen
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 1
Introduction
Flask is a web development framework developed in Python.
Flask won’t make many decisions for you, such as what database to
use. Those decisions that it does make, such as what templating
engine to use, are easy to change. Everything else is up to you, so
that Flask can be everything you need and nothing you don’t.
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 2
Why is Flask called a micro-framework?
Install Flask
$ pip install Flask
Install virtualenv
If you are using Python 2, the venv module is not available. You need
to install.
# Ubuntu
# CentOS, Fedora
On Windows, as an administrator:
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 3
Hello world Example
1) Create a folder and name it, You can give any name.
2) In created folder create a module (a python file) and name it, you
can give any name. Ex : Demo1.py
4) save the module and run the module using command prompt.
python module.py
* Environment: production
Output
Program Explanation
app = Flask(__name__)
@app.route('/')
def wish():
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 5
# main driver function
if __name__ == '__main__':
app.run()
Output
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 6
Routing
Use the route() decorator to bind a function to a URL.
@app.route('/')
def index():
@app.route('/hello')
def hello():
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 7
return html_text
@app.route('/two')
def page_two():
html_text = '''<html>
<head>
<title>Python With Naveen</title>
</head>
<body>
<h1>This is Page 2</h1>
</body>
</html>'''
return html_text
if __name__ == "__main__":
app.run()
Output
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 8
Variable Rules
You can add variable sections to a URL by marking sections with
<variable_name>.
@app.route('/user/<username>')
def show_user_profile(username):
@app.route('/post/<int:post_id>')
def show_post(post_id):
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 9
@app.route('/path/<path:subpath>')
def show_subpath(subpath):
Converter types:
Output
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 11
if __name__ == "__main__":
app.run(debug=True)
Output
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 12
Unique URLs /
Redirection
Behaviour
The following two rules differ in their use of a trailing slash.
@app.route('/projects/')
def projects():
@app.route('/about')
def about():
The canonical URL for the projects endpoint has a trailing slash. It’s
similar to a folder in a file system. If you access the URL without a
trailing slash, Flask redirects you to the canonical URL with the
trailing slash.
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 13
The canonical URL for the about endpoint does not have a trailing
slash. It’s similar to the pathname of a file. Accessing the URL with a
trailing slash produces a 404 “Not Found” error. This helps keep URLs
unique for these resources, which helps search engines avoid
indexing the same page twice.
Rendering
Templates
The Flask configures the Jinja2 template engine automatically.
Jinja2 : https://jinja.palletsprojects.com/en/2.11.x/
All you have to do is provide the name of the template and the
variables you want to pass to the template engine as keyword
arguments.
@app.route('/hello/')
@app.route('/hello/<name>')
def hello(name=None):
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 14
return render_template('hello.html', name=name)
Case 1: a module:
Case 2: a package:
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 15
Template
Designer
Jinga 2 template engine provides some delimiters which can be used
in the HTML to make it capable of dynamic data representation.
o {# ... #} for the comments that are not included in the template
output
Example
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 16
app = Flask(__name__)
@app.route('/<name>')
def showIndex(name):
return render_template("index.html",name=name)
if __name__ == "__main__":
app.run()
template
<html>
<head>
<title>Python With Naveen</title>
</head>
<body>
<style>
h1{
font-family: "Agency FB";
font-size: 40px;
background-color: red;
color: yellow;
text-align: center;
}
</style>
<h1>Welcome Mr/Miss : {{ name }}</h1>
</body>
</html>
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 17
Output
Static Files
To generate URLs for static files, use the special 'static' endpoint
name:
url_for('static', filename='style.css')
Example
app = Flask(__name__)
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 18
@app.route('/open/<name>')
def showtemplate(name):
return render_template('index.html',name=name)
if __name__ == "__main__":
app.run(debug=True)
template
<head>
<link rel="stylesheet" href="{{
url_for('static',filename='example.css')}}">
</head>
<body>
<h1>Welcome to Templates {{ name }}</h1>
<a href="http://www.facebook.com">Open Face Book</a>
</body>
</html>
example.css
h1{
text-align: center;
font-size: 60px;
background-color: blue;
color: yellow;
font-family: "Agency FB";
}
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 19
Pending Notes will be Updated in short
Thank you
Youtube : https://www.youtube.com/c/pythonwithnaveen
Facebook : https://www.facebook.com/groups/pythonwithnaveen/ Page 20