Flask
D:\>cd localy
D:\Localy>mkdir flasksite
D:\Localy>cd flasksite
D:\Localy\flasksite>mkvirtualenv flask
(flask) D:\Localy\flasksite>pip install flask
workon flask
python name.py
Create a Hello World progaram.
Open pyCharm Open project flasksite, and create new file name as app.py
Output:
Hello World.
New page:
Varioble by enter in urls:
For number in variobles.
You should create folder name "templates" in project file and add to any ".html" eg. index.html
templates name as, page.
htlm page:
outpyt is :
Yaseen You are loged out..
html template name as, lang.html
output:
html
css
boostrap
python
django
flask
TK
template name as code.html
base.html
program.py
D:\Localy>mkdir flasksite
D:\Localy>cd flasksite
D:\Localy\flasksite>mkvirtualenv flask
(flask) D:\Localy\flasksite>pip install flask
workon flask
python name.py
Create a Hello World progaram.
Open pyCharm Open project flasksite, and create new file name as app.py
from flask import Flask app=Flask(__name__) @app.route('/') def home(): return '<h1>Hello World.</h1>' app.run()
Hello World.
New page:
from flask import Flask app=Flask(__name__) @app.route('/new') def rew(): return '<h2>oh. this is new page</h2>' app.run()
Varioble by enter in urls:
from flask import Flask var=Flask(__name__) @var.route('/<username>') def print(username): return '<p> Hi %s</p>' %username var.run()
For number in variobles.
from flask import Flask var=Flask(__name__) @var.route('/<int:age>') def print(age): return '<p> Hi %d</p>' %age var.run()
from flask import Flask, render_template var=Flask(__name__) @var.route('/') def print(): return render_template('index.html') var.run(debug=True)
New templates with value passing.
Note: this program run only python 3.9 and above.from flask import Flask, render_template add=Flask(__name__) @add.route('/') def dis(): return '<h2>Hi all </h2>' @add.route('/<user>') def pro(user): return render_template('page.html', user=user) add.run(debug=True)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>new page</title> </head> <body> <h2>Welcome to new page....{{user}}</h2> </body> </html>
Logical oprations.
from flask import Flask, render_template add=Flask(__name__) @add.route('/') def dis(): return '<h2>Hi all </h2>' @add.route('/<user>') def pro(user): return render_template('page.html', user=user, isActive=False) add.run(debug=True)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>new page</title> </head> <body> {% if isActive %} <h2>{{user}}, Welcome to new page..</h2> {% else %} <h2>{{user}}, You are loged out..</h2> {% endif %} </body> </html>
List printing for loop using ul tagg.
from flask import Flask, render_template list=Flask(__name__) base= ['html', 'css', 'boostrap', 'python', 'django', 'flask', 'TK'] @list.route('/code') def coding(): return render_template('lang.html', base=base) list.run(debug=True)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>List</title> </head> <body> {% for i in base %} <ul> <li>{{i}}</li> </ul> {% endfor %} </body> </html>
css
boostrap
python
django
flask
TK
image and title using for loop and dictniory
from flask import Flask, render_template lang=Flask(__name__) programing=[{'name':'html', 'img': 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/61/HTML5_logo_and_wordmark.svg/1200px-HTML5_logo_and_wordmark.svg.png'}, {'name':'css', 'img':'https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/CSS3_logo_and_wordmark.svg/1200px-CSS3_logo_and_wordmark.svg.png'}, {'name':'JavaScript', 'img':'https://static.javatpoint.com/images/javascript/javascript_logo.png'}] @lang.route('/') def code(): return render_template ('code.html', programing=programing) lang.run(debug=True)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Coding Lang</title> </head> <body> {% for i in programing %} <ul> <li><img src="{{i.img}}" alt="" style="width:100px; height:100px">{{i.name}}</li> </ul> {% endfor %} </body> </html>
Final: Flask Projct
{% extends 'base.html' %} {% block body %} <!--Grid start--> <div class="container"> <div class="row"> <div class="col"> <!--Column start--> {% for i in programing %} <div class="card shadow" style="height: 30rem; width: 22rem; float: left; margin: 15px"> <img src="{{i.img}}" class="card-img-top" alt="{{i.name}}" style="height: 20rem; width: padding-left: 20px;" title="{{i.name}}"> <div class="card-body"> <h5 class="card-title">{{i.name}}</h5> <p class="card-text">{{i.rate}}</p> <a href="#" class="btn btn-primary">Enroll</a> </div> </div> {% endfor %} <!--Column start--> </div> </div> </div> <!--Grid end--> {% endblock %}
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>OnClass - online courses</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"> <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> </head> <body> <!--navbar--> <!-- As a heading --> <nav class="navbar navbar-dark bg-dark"> <div class="container-fluid"> <span class="navbar-brand mb-0 h1">OnClass</span> </div> </nav> <!--content--> {% block body %} {% endblock %} </body> </html>
from flask import Flask, render_template lang=Flask(__name__) programing=[{'name':'Html Course', 'img': 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/61/HTML5_logo_and_wordmark.svg/1200px-HTML5_logo_and_wordmark.svg.png', 'rate':'$400'}, {'name':'Css Course', 'img':'https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/CSS3_logo_and_wordmark.svg/1200px-CSS3_logo_and_wordmark.svg.png', 'rate':'$299'}, {'name':'JS Course', 'img':'https://www.thecodecrypt.com/images/2020/01/08/js.png', 'rate':'$ 1249'}] @lang.route('/') def code(): return render_template ('code.html', programing=programing) lang.run(debug=True)
No comments