Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/gurupatil0003/01_flask_tutorial

Flask is a lightweight and flexible web framework for Python. It's designed to make getting started with web development in Python quick and easy, with the ability to scale up to complex applications. Flask was created by Armin Ronacher and first released in 2010.
https://github.com/gurupatil0003/01_flask_tutorial

backend database flask front-end-development html-css-javascript python

Last synced: about 20 hours ago
JSON representation

Flask is a lightweight and flexible web framework for Python. It's designed to make getting started with web development in Python quick and easy, with the ability to scale up to complex applications. Flask was created by Armin Ronacher and first released in 2010.

Awesome Lists containing this project

README

        

## Topics

- [What is Flask](#what-is-flask)
- [Install Virtual Environment](#Install-Virtual-Environment)
- [How to Install Flask](#install-flask)
- [How check Flask Version](#Pip-version)
- [A mini_Flask application](#a-minimal-app)
- [Flask Project Structure](#flask-project-structure) - a few options
- (WIP) [Flask Bootstrap Sample](#flask-bootstrap-sample) - a simple project built with Bootstrap
- (WIP) [Jinja Template](#jinja-template) - how to render HTML pages efficiently
- [Url_Variable Rule and Creatinng_url_Examle](#Url-Rule-&-Creation-url)-What is the Rule of Variable and creation of Url


## What is Flask

*Flask is a lightweight web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications.
Compared to [Django](https://www.djangoproject.com/), Flask provides a lightweight codebase and more freedom to the developer.*

## Virtual Environment
*-We use a module named virtualenv which is a tool to create isolated Python environments. virtualenv creates a folder that contains all the necessary executables to use the packages that a Python project would need.*

*-A Python Virtual Environment is an isolated space where you can work on your Python projects, separately from your system-installed Python.*

*-You can set up your own libraries and dependencies without affecting the system Python.*

*-We will use virtualenv to create a virtual environment in Python.*

## Why do we need a virtual environment?

*-Imagine a scenario where you are working on two web-based Python projects one of them uses Django 4.0 and the other uses Django 4.1 (check for the latest Django versions and so on). In such situations, we need to create a virtual environment in Python that can be really useful to maintain the dependencies of both projects.*

### Installing Virtula environment
```python
#Installing Virtula environment
pip install virtualenv
```
### Call Virutal Environment
```python
#Call Virutal Environment and it's local directory Name Like--- Venv or Guru or Data etc....
virtualenv venv
```
### Activate a virtual environment
```python
#Activate a virtual environment based on your OS
For windows > venv\Scripts\activate
For linux > source ./venv/bin/activate

```
#If your Facing Any error Then Try this
```python
Set-ExecutionPolicy RemoteSigned -Scope Process

```
## Install Flask

*The easiest way to install [Flask](https://palletsprojects.com/p/flask/) is to use [PIP](https://pip.pypa.io/en/stable/quickstart/) the official package-management tool.*

### Install Flask Command
```python
pip install Flask
```


## How to check Flask version

*Open a Python console or platoform (type python in terminal) and check the installed version as below:*

```python

import flask
flask_version = flask.__version__
print(f"Installed Flask version: {flask_version}")

Or
flask --version

```
# Mini App

[Code Here ⚙️](/minimal_app)

```python
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
return "

Hello, World!

"

if __name__=="__main__":
app.run()

```

#### So what did that code do?

*1. First we imported the Flask class. An instance of this class will be our WSGI application.*

*2. Next we create an instance of this class. The first argument is the name of the application’s module or package. __name__ is a convenient shortcut for this that is appropriate for most cases. This is needed so that Flask knows where to look for resources such as templates and static files.*

*3. We then use the route() decorator to tell Flask what URL should trigger our function.*

*4. The function returns the message we want to display in the user’s browser. The default content type is HTML, so HTML in the string will be rendered by the browser.*


# Debug Mode

[Code Here ⚙️](/debug_mode)

```python
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
return "

Hello, World!

"

# debug mode running on 8000 port
if __name__=="__main__":
app.run(debug=True, port=8000)
```
*> The flask run command can do more than just start the development server. By enabling debug mode, the server will automatically reload if code changes, and will show an interactive debugger in the browser if an error occurs during a request.*

*> Warning ⚠️*
*> The debugger allows executing arbitrary Python code from the browser. It is protected by a pin, but still represents a major security risk. Do not run the development server or debugger in a production environment.*



# Creation Routing App

[Code Here ⚙️](/routing)

```python
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
return 'This is Index Page'

@app.route('/login')
def login():
return 'This is Login Page'

@app.route('/hello')
def hello():
return 'Hello, World'

if __name__=="__main__":
app.run(debug=True)

```

> Modern web applications use meaningful URLs to help users. Users are more likely to like a page and come back if the page uses a meaningful URL they can remember and use to directly visit a page.

> Use the `route()` decorator to bind a function to a URL.

## Rendering Templates

[Code Here ⚙️](/render_template)

```python
from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def index():
return render_template('index.html')

@app.route("/")
def about():
return render_template('about.html')

if __name__=="__main__":
app.run()

```

```python
python filename.py
or
start flask

```

#### In flask, html file are served from the 'templates' folder by default and all the static file; images, css, js, etc are served from the 'static' folder.

> These folders should be present in the root directly of your python application



structure




## Crl Rule and Url Creation

[Code Here ⚙️](/url_variables/README.md)

```python
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
return render_template('index.html')

# string
@app.route('/string/')
def string(value):
return f"

Hi this is a string value {value}

"

# int
@app.route('/int/')
def int(value):
return f"

Hi this is a int value {value}

"

# float
@app.route('/float/')
def float(value):
return f"

Hi this is a float value {value}

"

# path
@app.route('/path/')
def path(value):
return f"

Hi this is a path value {value}

"

# uuid
@app.route('/uuid/')
def uuid(value):
return f"

Hi this is a uuid value {value}

"

if __name__=="__main__":
app.run(debug=True)

```

~~~python



Flask Form Example

Enter Your Name




Submit

~~~

~~~python
from flask import Flask, request, render_template

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
name = request.form['name'] # Get data from form
return f"

Hello, {name}!

Go Back"
return render_template('index.html') # Render HTML form

if __name__ == '__main__':
app.run(debug=True)

~~~

# Lab Register and Login page

```python
from flask import Flask, render_template, request, redirect, url_for, flash
from werkzeug.security import generate_password_hash, check_password_hash

app = Flask(__name__)
app.secret_key = 'your_secret_key' # For flash messages

# In-memory user storage (for simplicity)
users = {}

@app.route('/')
def home():
return render_template('index.html')

@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']

if username in users and check_password_hash(users[username], password):
return redirect(url_for('home'))
else:
flash('Invalid username or password', 'error')
return render_template('login.html')

@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']

if username in users:
flash('Username already taken', 'error')
else:
users[username] = generate_password_hash(password)
return redirect(url_for('login'))

return render_template('register.html')

if __name__ == '__main__':
app.run(debug=True)

```

# index Page

```python



Home

Welcome!


Login | Register

```

#index page css
~~~python

body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
text-align: center;
margin-top: 50px;
}

h1 {
color: #333;
}

a {
text-decoration: none;
color: #007BFF;
font-weight: bold;
margin: 10px;
}

a:hover {
color: #0056b3;
}

~~~

# Login Page
```python

Login

Username:

Password:

Login

Don't have an account? Register here

```

# Register Page

```python

Register

Username:

Password:

Register

Already have an account? Login here

```
# Register and login page with Databse

```python
from flask import Flask, render_template, request, redirect, url_for, flash
import sqlite3
from werkzeug.security import generate_password_hash, check_password_hash

app = Flask(__name__)
app.secret_key = 'your_secret_key' # For flash messages

# Database setup
def init_db():
with sqlite3.connect('users.db') as conn:
conn.execute('''CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL)''')
conn.commit()

init_db()

# Helper function to query the database for user details
def get_user_by_username(username):
with sqlite3.connect('users.db') as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE username = ?", (username,))
return cursor.fetchone()

# Helper function to add a user to the database
def add_user(username, password):
with sqlite3.connect('users.db') as conn:
cursor = conn.cursor()
cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
conn.commit()

@app.route('/')
def home():
return render_template('index.html')

@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']

user = get_user_by_username(username)
if user and check_password_hash(user[2], password): # Check if password matches
return redirect(url_for('home'))
else:
flash('Invalid username or password', 'error')
return render_template('login.html')

@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']

# Check if the username already exists
if get_user_by_username(username):
flash('Username already taken', 'error')
else:
hashed_password = generate_password_hash(password)
add_user(username, hashed_password)
return redirect(url_for('login'))

return render_template('register.html')

if __name__ == '__main__':
app.run(debug=True)

```

# Index Page
```python

Welcome!


Login | Register

````

# login Page

```python

Login

Username:

Password:

Login

Don't have an account? Register here

```

# Register Page

```python

Register

Username:

Password:

Register

Already have an account? Login here

```

# Login Redirect to home page

```python

from flask import Flask, render_template, request, redirect, url_for, flash
import sqlite3
from werkzeug.security import generate_password_hash, check_password_hash

app = Flask(__name__)
app.secret_key = 'your_secret_key' # For flash messages

# Database setup
def init_db():
with sqlite3.connect('users.db') as conn:
conn.execute('''CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL)''')
conn.commit()

init_db()

# Helper function to query the database for user details
def get_user_by_username(username):
with sqlite3.connect('users.db') as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE username = ?", (username,))
return cursor.fetchone()

# Helper function to add a user to the database
def add_user(username, password):
with sqlite3.connect('users.db') as conn:
cursor = conn.cursor()
cursor.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
conn.commit()

@app.route('/')
def home():
return render_template('index.html')

@app.route('/home2')
def home2():
# Make sure the user is logged in (for simplicity, we are not implementing sessions here)
return render_template('home2.html')

@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']

user = get_user_by_username(username)
if user and check_password_hash(user[2], password): # Check if password matches
return redirect(url_for('home2')) # Redirect to home2 after login
else:
flash('Invalid username or password', 'error')
return render_template('login.html')

@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']

# Check if the username already exists
if get_user_by_username(username):
flash('Username already taken', 'error')
else:
hashed_password = generate_password_hash(password)
add_user(username, hashed_password)
return redirect(url_for('login'))

return render_template('register.html')

if __name__ == '__main__':
app.run(debug=True)
```
# index Html
```html

Welcome!


Login | Register

```
# Login Html

```python

Login

Username:

Password:

Login

Don't have an account? Register here

```

# Register Html

```html

Register

Username:

Password:

Register

Already have an account? Login here

```

# Home2.Html

```python



Home - Welcome!


Welcome, you are logged in!


This is a colorful page. Enjoy your session!


Go back to the main page


```