You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
a. Static folder is public in nature.
b. Templates folder is private in nature.
c. All the web components are created in the templates folder.(html, css, js)
Importing the and Using of teh templates folder:
a. " from flask import Flask, render_template "command is used to import the templates.
b. We use the returnfunctionto show the templates folder during the app run.
c. return render_template('index.html')
d. We can also use multiple html files by adding the route.
e. @app.route("/name of the route")
Dyanmaically updating the preview during the production:
app.run(debug=True){ for real time changes}
Passing of Variables to the html file:
a. Firstly in the app.py:
variable="Rahul"return render_template('index.html', name=variable)
b. Then to display it in the html file:
{{name}} inside any tag
4. Utilization of the Static folder:
Using the jinja(url_for):
Normal way:
<img src=”static/img_name.png”>
using the jinja:
<img src="{{ url_for('static', filename='img_name.png') }}">
OR
<link href="{{ url_for('static', filename='css/main.css') }}" rel="stylesheet">
a. Keep the admin and the password default
b. Default Admin will be "root"
c. Default password will be "blank"
Starting the Xampp Control panel:
a. Start the "Apache".
b. Start the "MySQl".
c. Everything should turn green over the apche and mysql.
d. If Something goes wrong then check ( Task Manager>>Details and check the PID).
e. End task armsvc.exe and everything should be fine.
a. Go to http://localhost/phpmyadmin/ link, you will see a interface.
b. Click on “New” to create a database
c. Write your database name
d. Create the database
Create a new Table in the database:
a. Click on “Create”.
b. Fill the table according to need.
c. A_I section for thr primary key. A_I stands for Auto Incrementation. It means it will automatically increment value so that there is no repetition.
8. Flask SQLAlchemy and inputing data to Database:
Install flask-sqlalchemy in the system.
pip install flask-sqlalchemy
or
pip install flask_sqlalchemy
Import this module in the python file.
from flask_sqlalchemy import SQLAlchemy
To connect with the database first we have to give database’s address.
If do not set the user or the password during the installtion of the xammp the default values are:
username: root
password: BLANK
db_name: is the database name which we have setup
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost/DATABASENAME'
It should look shomething like this once configured.
Importing the class and object from the sqlalchemy and to do so we have to make a variable:
db = SQLAlchemy(app)
Once we have connected to the database we need to access the table in the python file:
class Contacts(db.Model):
sno = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
phone_num = db.Column(db.String(12), nullable=False)
msg = db.Column(db.String(120), nullable=False)
date = db.Column(db.String(12), nullable=True)
email = db.Column(db.String(20), nullable=False)
@app.route("/contact", methods = ['GET', 'POST'])
def contact():
if(request.method=='POST'):
'''Fetch data and add it to the database'''return render_template('index.html')
Fetch data from the form and send to the database:
a. First add name attribute to the html:
<input name="email" type="email" placeholder="Email Address">
b. We use the name attribute to locate the data from the form and send to data base:
@app.route("/contact", methods = ['GET', 'POST'])
def contact():
if(request.method=='POST'):
'''Add entry to the database'''
name = request.form.get('name')
email = request.form.get('email')
phone = request.form.get('phone')
message = request.form.get('message')
entry = Contacts(name=name, phone_num = phone, msg = message, date=
datetime.now(),email = email )
db.session.add(entry)
db.session.commit()
return render_template('contact.html')
c. Now to we need to add method and action in te html file, like this:
<form name="sentMessage" action = "/contact" method="post" novalidate>
After everything the main.py file should look something like this :
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost/codingthunder'
db = SQLAlchemy(app)
class Contacts(db.Model):
sno = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
phone_num = db.Column(db.String(12), nullable=False)
msg = db.Column(db.String(120), nullable=False)
date = db.Column(db.String(12), nullable=True)
email = db.Column(db.String(20), nullable=False)
@app.route("/", methods = ['GET', 'POST'])
def contact():
if(request.method=='POST'):
'''Add entry to the database'''
name = request.form.get('name')
email = request.form.get('email')
phone = request.form.get('phone')
message = request.form.get('message')
entry = Contacts(name=name, phone_num = phone, msg = message, date= datetime.now(),email = email )
db.session.add(entry)
db.session.commit()
return render_template('contact.html')
app.run(debug=True)
10. Making Parameters Configurable in Flask:
We will configure the Paramerters using the config.json .
Firtsly we will create a config.json in the project directory.
In the json file we will implement the configurationa s follows:
{
"params": {
"websitename": "Flask-Project"
}
}
In the main.py file we will import the json file as follows:
import json
with open('config.json', 'r') as c:
params = json.load(c)["params"]
Now we can simply use the json file to pass the parameters as we pass the variables:
params['websitename']
Passing the json file to html to implement the configuration: