Flintor is an ultra-lightweight, dependency-free web framework in Python. It provides core functionalities similar to Flask and Django but remains minimal and efficient. This documentation will guide you through installation, usage, and the various features offered by Flintor.
- Features
- Installation
- Getting Started
- Routing
- Request and Response Objects
- Template Rendering
- Middleware
- Static Files
- Sessions
- Database ORM
- Full Example Application
- Advanced Topics
- FAQ
- Contributing
- License
- Routing with Decorators: Define routes using Python decorators.
- Template Rendering: Use the built-in
string.Template
for simple templating. - Request and Response Objects: Encapsulate HTTP request and response data.
- Middleware Support: Add middleware functions for request processing.
- Static File Serving: Serve static files like CSS, JS, and images.
- Simple ORM-like Functionality: Interact with a SQLite database.
- Session Management: Manage user sessions with cookies.
Note: Flintor is designed to be installed via pip
once it's uploaded to PyPI. For now, you can install it locally.
bash
pip install flintor
(Assuming the package is uploaded to PyPI under the name flintor
.)
-
Clone the repository or download the source code.
-
Navigate to the root directory of the project (where
setup.py
is located). -
Install the package using:
bash
pip install .
Create a file named app.py
and add the following code:
python
`from flintor import Framework
app = Framework()
@app.route('/') def index(request): return 'Hello, World!'
if name == 'main': app.run()`
Run the application:
bash
python app.py
Visit http://127.0.0.1:8000/
in your browser to see "Hello, World!".
Use the @app.route()
decorator to define routes.
python
@app.route('/path') def handler(request): return 'Response'
Include dynamic parameters in the URL using angle brackets.
python
@app.route('/user/<username>') def show_user_profile(request, username): return f"User: {username}"
Specify allowed HTTP methods using the methods
parameter.
python
@app.route('/submit', methods=['GET', 'POST']) def submit(request): if request.method == 'POST': # Handle POST request pass else: # Handle GET request pass
The Request
object provides access to request data.
request.method
: HTTP method (e.g., 'GET', 'POST').request.path
: The path of the request URL.request.query
: Dictionary of query parameters.request.headers
: HTTP headers.request.cookies
: Cookies sent by the client.request.form
: Dictionary of form data (for POST requests).request.body
: Raw request body.
python
value = request.query.get('key', ['default'])[0]
Use the Response
object to customize the response.
python
`from flintor import Response
@app.route('/custom') def custom_response(request): response = Response( body='Custom Response', status=200, headers={'Content-Type': 'text/plain'} ) response.set_cookie('sessionid', 'abc123') return response`
Render templates using the render_template
method.
Create templates in the templates/
directory.
templates/hello.html
html
`
<title>$title</title> `python
@app.route('/hello/<name>') def hello(request, name): return app.render_template('hello.html', title='Greeting', name=name)
Middleware functions process requests before they reach route handlers.
python
`def simple_middleware(request, response): print(f"{request.method} request for {request.path}") # Return None to continue processing return None
app.add_middleware(simple_middleware)`
Serve static files from the static/
directory.
In your templates:
html
<link rel="stylesheet" type="text/css" href="/static/style.css">
Static files are automatically served when requested with the /static/
path.
Manage user sessions using cookies.
python
@app.route('/login', methods=['POST']) def login(request): username = request.form.get('username', [''])[0] request.session['user'] = username return 'Logged in'
python
@app.route('/dashboard') def dashboard(request): user = request.session.get('user', 'Guest') return f'Welcome, {user}'
Interact with a SQLite database using the ORM
class.
python
db = ORM('mydatabase.db')
python
`# Create a table db.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, username TEXT)', commit=True)
db.execute('INSERT INTO users (username) VALUES (?)', (username,), commit=True)
users = db.fetchall('SELECT * FROM users')`
python
db.close()
Project Structure
arduino
your_app/ ├── app.py ├── templates/ │ ├── index.html │ ├── hello.html ├── static/ │ └── style.css
app.py
python
`from flintor import Framework, Response
app = Framework()
@app.route('/') def index(request): return app.render_template('index.html', title='Home', message='Welcome!')
@app.route('/hello/') def hello(request, name): return app.render_template('hello.html', title='Greeting', name=name)
def logger_middleware(request, response): print(f"{request.method} {request.path}") return None
app.add_middleware(logger_middleware)
if name == 'main': app.run()`
templates/index.html
html
`
<title>$title</title> `templates/hello.html
html
`
<title>$title</title> `Customize error responses by overriding the send_error
method in the RequestHandler
class.
python
`def custom_send_error(self, code, message=None): self.send_response(code) self.end_headers() self.wfile.write(f"
{message}
".encode())def _make_handler(self): # Existing code... class RequestHandler(http.server.BaseHTTPRequestHandler): # Existing methods...
def send_error(self, code, message=None):
custom_send_error(self, code, message)`
- Template Engine: Integrate a more powerful template engine like Jinja2.
- Database Models: Build an ORM layer for model representations.
- Security: Implement authentication and authorization mechanisms.
The included ORM uses SQLite for simplicity. You can extend the ORM
class to support other databases or integrate a full-featured ORM like SQLAlchemy.
Any files placed in the static/
directory can be served. Just ensure they're accessible via the correct URL path.
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Make your changes.
- Submit a pull request.
Remember, I am not God. I have not thought of everything, if you have an idea on what to do, please do it. I would love to incorportate it.
Please ensure your code follows good coding practices and includes documentation/comments where necessary.
Side note: If someone would be willing to clean up this readme with proper markdown format, it would be awesome. I will be getting to that later.