📅  最后修改于: 2023-12-03 15:00:43.974000             🧑  作者: Mango
Fadaei - Python is a lightweight and high-performance Python web framework with simplicity, minimalism, and strong modularization in mind. It lets you build web apps quickly and easily with much less code. With Fadaei - Python, you can build your web app with the following features.
Fadaei - Python helps you to build web apps with just a few lines of code. Its excellent documentation, ease of use, and versatility make it one of the most popular frameworks to have in your toolkit.
Fadaei - Python comes with an easy-to-use CLI to help you set up a new project with minimal efforts. To install Fadaei - Python, you first need to have Python and pip installed on your system. Once you have them installed, you can use the following command to install Fadaei - Python.
pip install fadaei
After you have installed Fadaei - Python, you can create a new project by running the following command:
fadaei startproject myproject
This will create a new project called myproject with the following structure:
myproject/
│
├── controllers/
│ └── __init__.py
│
├── models/
│ └── __init__.py
│
├── templates/
│ └── __init__.py
│
├── static/
│ └── css/
│ └── js/
│ └── img/
│
├── main.py
├── settings.py
├── urls.py
└── requirements.txt
Now you can run your project by executing the following command:
python main.py
This will start your server, and you can view your web app by navigating to http://localhost/
in your web browser.
Routing is the process of matching a URL with a corresponding view function. Fadaei - Python comes with a simple and intuitive method of routing.
from fadaei import Fadaei
app = Fadaei()
@app.route("/")
def index():
return "Hello, World!"
In this example, we create a new Fadaei - Python application and then define a new route using the @app.route
decorator. The index
function is assigned as the view function for the /
route. When a user visits http://localhost/
, the index
function will be called, and the string "Hello, World!"
will be returned.
Fadaei - Python comes with a built-in templating engine called Jinja2 which is a modern and powerful web template engine for Python.
from fadaei import Fadaei, render_template
app = Fadaei()
@app.route("/")
def index():
return render_template("index.html", name="World")
In this example, we create a new route which renders the index.html
template. The render_template
function passes the variables name = "World"
to the index.html
template. Then, the index.html
template can use these variables to render dynamic content.
<!doctype html>
<html>
<head>
<title>Hello, {{ name }}!</title>
</head>
<body>
<h1>Hello, {{ name }}!</h1>
</body>
</html>
The index.html
template uses Jinja2 syntax to access the name
variable and render it dynamically.
Middleware in Fadaei - Python is a way to modify or inspect incoming requests and outgoing responses. You can add one or more middleware functions to your application to handle tasks like logging, authentication, and more.
from fadaei import Fadaei
app = Fadaei()
@app.middleware
async def my_middleware(request, handler):
print("Middleware before handling request")
response = await handler(request)
print("Middleware after processing request")
return response
@app.route("/")
def index():
return "Hello, World!"
In this example, we create a new middleware function my_middleware()
and add it to our application using the @app.middleware
decorator. The my_middleware()
function is called every time a request is made to our application.
Fadaei - Python supports various ORMs like SQLAlchemy, Peewee, Tortoise-ORM, and Pony ORM.
from fadaei import Fadaei
from tortoise import fields
from tortoise.models import Model
app = Fadaei()
class User(Model):
name = fields.CharField(max_length=255)
email = fields.CharField(max_length=255, unique=True)
password = fields.CharField(max_length=255)
class Meta:
table = "users"
@app.route("/")
def index():
users = User.all()
return render_template("index.html", users=users)
In this example, we create a new User
model which inherits from the Model
class. Then we define the fields email, name, and password using the Tortoise-ORM. Now we can retrieve all users from the database and render them into the template.
Web applications often require the use of sessions, which allow you to store user data across requests. Fadaei - Python provides a simple API for working with sessions.
from fadaei import Fadaei, Session
app = Fadaei()
@app.route("/")
def index():
session = Session(request)
if "name" in session:
name = session.get("name")
return f"Hello {name}!"
return "Please log in"
@app.route("/login")
def login():
session = Session(request)
session["name"] = "John Doe"
return "Logged in!"
@app.route("/logout")
def logout():
session = Session(request)
session.delete("name")
return "Logged out!"
In this example, we create three different routes: /
, /login
, and /logout
. The index
route checks if the session contains a name
variable and then renders it into the template. The login
route sets the name
session variable to "John Doe". Finally, the logout
route deletes the name
session variable.
Fadaei - Python provides a unique way to upload and serve files using the built-in static
directory.
from fadaei import Fadaei, request, send_file
app = Fadaei()
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
file = request.files["file"]
file.save(f"static/{file.filename}")
return render_template("index.html")
@app.route("/download/<filename>")
def download(filename):
return send_file(f"static/{filename}")
In this example, we create two routes: /
and /download/<filename>
. The /
route handles file uploads by using the request.files
object and saving the uploaded file to the static
directory. The /download/<filename>
route serves the requested file from the static
directory using the send_file
method.
Fadaei - Python provides a powerful CLI interface to automate various tasks like creating a new project, managing database migrations, and more.
usage: fadaei [-h] [--version] {startproject,list,routes,shell,makemigrations,migrate} ...
Fadaei - Python CLI
positional arguments:
{startproject,list,routes,shell,makemigrations,migrate}
startproject Creates a new Fadaei - Python project
list Lists all available routes
routes Prints all registered routes
shell Starts an interactive Fadaei - Python shell
makemigrations Create new database migrations
migrate Run database migrations
optional arguments:
-h, --help show this help message and exit
--version show program's version number and exit
In this example, we see the available commands in the Fadaei - Python CLI. We can create a new project by running fadaei startproject myproject
. We can list all the available routes by running fadaei list
. We can check all routes' registered functions by running fadaei routes
. We can start an interactive Fadaei - Python shell by running fadaei shell
. We can create new database migrations by running fadaei makemigrations
. We can run database migrations by running fadaei migrate
.
Fadaei - Python is an excellent Python web framework that provides a simple and easy-to-use interface for developing web apps. Its built-in libraries, features, and extensions are designed to enable developers to create quality applications quickly and efficiently. With its focus on simplicity, minimalism, and strong modularization, Fadaei - Python is a perfect choice for building modern web apps.