Skip to main content

Mini Framework for Human

Project description

๐Ÿฆˆ PieShark Framework

A Lightweight, Powerful Python Web Framework


๐Ÿš€ Overview

PieShark is a modern, lightweight WSGI-based Python web framework designed for developers who want power without complexity. Built with performance and security in mind, it offers everything you need to create robust web applications and APIs.

โœจ Key Features

  • ๐Ÿ›ฃ๏ธ Flexible Routing - Path parameters, regex patterns, and RESTful endpoints
  • ๐Ÿ” Built-in Security - Automatic session and cookie encryption
  • ๐Ÿงฉ Modular Design - Blueprint system for organized code architecture
  • โšก Async/Sync Support - Native support for both synchronous and asynchronous handlers
  • ๐ŸŽจ Simple Templating - Lightweight template engine with Python variable injection
  • ๐Ÿ”ง Middleware Ready - Custom middleware support for request/response processing
  • ๐Ÿ“ฆ Static File Serving - Built-in static file handling

๐Ÿ Quick Start

Installation & Setup

from PieShark.main import pieshark, request, form, session
from PieShark.blueprint import Blueprint
from PieShark.session import SESSION
from PieShark.cookie import Cookie
from PieShark.templates import Templates
from PieShark.pydejs import PY_deJS

# Initialize your application
app = pieshark(debug=True)

# Configure your app
app.config.update({
    'secret_key': 'your-super-secret-key-here',
    'session_permanent': True,
    'url_dbase': 'base://sqlite3:static',
    'limit_size_upload': 3089003  # ~3MB upload limit
})

Your First Route

@app.route("/")
async def home():
    session['user'] = 'Developer'
    return Templates(
        "<h1>Welcome to {{ framework }}!</h1>", 
        framework="PieShark"
    )

@app.route("/api/user/{user_id}")
async def get_user(user_id):
    return app.json_response({
        "user_id": user_id,
        "status": "active"
    })

๐Ÿ›ฃ๏ธ Advanced Routing

PieShark supports multiple routing patterns to fit your application's needs:

Dynamic Parameters

@app.route("/users/{user_id}/posts/{post_id}")
async def get_user_post(user_id, post_id):
    return f"User {user_id}, Post {post_id}"

HTTP Methods

@app.route("/api/data", methods=["GET", "POST", "PUT", "DELETE"])
async def handle_data():
    if request.method == "POST":
        # Handle POST data
        return app.json_response({"created": True})
    elif request.method == "GET":
        # Handle GET request
        return app.json_response({"data": "sample"})

๐Ÿ” Security Features

Session Management

@app.route("/login", methods=["POST"])
async def login():
    # Sessions are automatically encrypted
    session['user_id'] = 12345
    session['username'] = 'john_doe'
    session['roles'] = ['user', 'admin']
    return app.redirect("/dashboard")

@app.route("/dashboard")
async def dashboard():
    if not session.get('user_id'):
        return app.abort(401)
    return f"Welcome back, {session['username']}!"

Encrypted Cookies

# Initialize cookie handler with custom salt
cookie = Cookie(salt="your_custom_salt", app=app)

@app.route("/set-preference")
async def set_preference():
    # Cookies are automatically encrypted
    cookie.create("theme=dark&lang=en")
    return "Preferences saved!"

@app.route("/get-preference")
async def get_preference():
    # Decrypt and read cookie data
    theme = cookie.select.theme.decode()
    return f"Current theme: {theme}"

๐Ÿงฉ Modular Architecture with Blueprints

Organize your application into logical modules:

# auth_blueprint.py
auth_bp = Blueprint('auth', url_prefix='/auth')

@auth_bp.route('/login', methods=['GET', 'POST'])
async def login():
    if request.method == 'POST':
        username = form.username.decode()
        password = form.password.decode()
        
        # Your authentication logic here
        if authenticate(username, password):
            session['user'] = username
            return app.redirect('/dashboard')
    
    return Templates("""
        <form method="post">
            <input name="username" type="text" placeholder="Username" required>
            <input name="password" type="password" placeholder="Password" required>
            <button type="submit">Login</button>
        </form>
    """)

@auth_bp.route('/logout')
async def logout():
    session.clear()
    return app.redirect('/')

# Register blueprint in main app
app.register_blueprint(auth_bp)

๐ŸŽจ Template System

Inline Templates

@app.route("/profile/{username}")
async def profile(username):
    user_data = get_user_data(username)  # Your data fetching logic
    
    return Templates("""
        <div class="profile">
            <h1>{{ user.name }}</h1>
            <p>Email: {{ user.email }}</p>
            <p>Joined: {{ user.join_date }}</p>
        </div>
    """, user=user_data)

File-based Templates

# For larger templates, use external files
@app.route("/complex-page")
async def complex_page():
    return Templates("complex_template.shark", 
                    title="My App",
                    data=fetch_page_data())

๐Ÿ“ Static File Handling

# Serve static files
app.static("static/", "/static/")
app.static("uploads/", "/files/")

# Now accessible at:
# /static/style.css -> static/style.css
# /files/document.pdf -> uploads/document.pdf

๐Ÿ“ค Form Processing & File Uploads

@app.route("/upload", methods=["GET", "POST"])
async def upload_file():
    if request.method == "POST":
        # Access form data (automatically encrypted)
        title = form.title.decode()
        description = form.description.decode()
        
        # Handle file uploads
        uploaded_file = form.file  # File object
        if uploaded_file:
            filename = secure_filename(uploaded_file.filename)
            ///Logic your saving file
        
        return app.json_response({
            "status": "success",
            "title": title,
            "filename": filename
        })
    
    return Templates("""
        <form method="post" enctype="multipart/form-data">
            <input name="title" type="text" placeholder="Title" required>
            <textarea name="description" placeholder="Description"></textarea>
            <input name="file" type="file" required>
            <button type="submit">Upload</button>
        </form>
    """)

๐Ÿ”„ Middleware & Hooks

Request Lifecycle Hooks

@app.before_request
def authenticate_user():
    """Run before every request"""
    if request.path.startswith('/admin/'):
        if not session.get('is_admin'):
            return app.abort(403)

@app.after_request
def add_security_headers(response):
    """Run after every request"""
    response.headers['X-Content-Type-Options'] = 'nosniff'
    response.headers['X-Frame-Options'] = 'DENY'
    return response

Custom Middleware

class RequestTimingMiddleware:
    def __init__(self, app):
        self.app = app
    
    def __call__(self, environ, start_response):
        start_time = time.time()
        response = self.app(environ, start_response)
        end_time = time.time()
        
        print(f"Request took {end_time - start_time:.2f}s")
        return response

# Apply middleware
app.wsgi_app = RequestTimingMiddleware(app.wsgi_app)

๐ŸŒ API Development

RESTful API Example

api_bp = Blueprint('api', url_prefix='/api/v1')

@api_bp.route('/users', methods=['GET'])
async def list_users():
    users = get_all_users()  # Your data logic
    return app.json_response({
        "users": users,
        "count": len(users)
    })

@api_bp.route('/users/{user_id}', methods=['GET', 'PUT', 'DELETE'])
async def handle_user(user_id):
    if request.method == 'GET':
        user = get_user_by_id(user_id)
        if not user:
            return app.json_response({"error": "User not found"}, status=404)
        return app.json_response(user)
    
    elif request.method == 'PUT':
        data = request.get_json()
        updated_user = update_user(user_id, data)
        return app.json_response(updated_user)
    
    elif request.method == 'DELETE':
        delete_user(user_id)
        return app.json_response({"message": "User deleted"})

app.register_blueprint(api_bp)

๐Ÿ› ๏ธ Utilities & Helpers

CDN Resource Fetcher (PyDeJS)

deJS = PY_deJS()

# Search for JavaScript libraries
jquery_results = deJS.get(search="jquery", limit=1)
minified_jquery = deJS.select("min")

# Use in templates
@app.route("/")
async def home():
    return Templates("""
        <script src="{{ jquery_url }}"></script>
        <h1>jQuery Loaded!</h1>
    """, jquery_url=minified_jquery)

๐Ÿš€ Deployment

Development Server

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8000, debug=True)

Production Deployment

Using Gunicorn

pip install gunicorn
gunicorn -w 4 -b 0.0.0.0:8000 app:app

Using Waitress

pip install waitress
waitress-serve --host=0.0.0.0 --port=8000 app:app

Nginx Configuration Example

server {
    listen 80;
    server_name yourdomain.com;
    
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    
    location /static/ {
        alias /path/to/your/static/files/;
        expires 30d;
    }
}

๐Ÿ“‹ Best Practices

Project Structure

your_project/
โ”‚
โ”œโ”€โ”€ app.py                 # Application entry point
โ”œโ”€โ”€ config.py              # Configuration settings
โ”œโ”€โ”€ requirements.txt       # Dependencies
โ”‚
โ”œโ”€โ”€ blueprints/            # Application modules
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ auth.py
โ”‚   โ”œโ”€โ”€ api.py
โ”‚   โ””โ”€โ”€ admin.py
โ”‚
โ”œโ”€โ”€ static/                # Static assets
โ”‚   โ”œโ”€โ”€ css/
โ”‚   โ”œโ”€โ”€ js/
โ”‚   โ””โ”€โ”€ images/
โ”‚
โ”œโ”€โ”€ templates/             # Template files
โ”‚   โ”œโ”€โ”€ base.shark
โ”‚   โ”œโ”€โ”€ home.shark
โ”‚   โ””โ”€โ”€ auth/
โ”‚       โ”œโ”€โ”€ login.shark
โ”‚       โ””โ”€โ”€ register.shark
โ”‚
โ”œโ”€โ”€ utils/                 # Utility functions
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ auth.py
โ”‚   โ””โ”€โ”€ helpers.py
โ”‚
โ””โ”€โ”€ tests/                 # Test files
    โ”œโ”€โ”€ __init__.py
    โ”œโ”€โ”€ test_auth.py
    โ””โ”€โ”€ test_api.py

Security Checklist

  • โœ… Use strong, unique secret_key
  • โœ… Store sensitive config in environment variables
  • โœ… Validate all user inputs
  • โœ… Use HTTPS in production
  • โœ… Implement proper error handling
  • โœ… Set appropriate file upload limits
  • โœ… Regular security updates

Performance Tips

  • Use async handlers for I/O-bound operations
  • Implement caching for frequently accessed data
  • Optimize database queries
  • Use CDN for static assets
  • Enable gzip compression
  • Monitor application performance

๐Ÿค Contributing

PieShark is designed to be lightweight yet powerful. Whether you're building a simple API, a complex web application, or a microservice, PieShark provides the tools you need without the bloat.

Why Choose PieShark?

Feature PieShark Other Frameworks
Learning Curve Minimal Steep
Built-in Security โœ… Encrypted sessions/cookies โŒ Manual setup
Async Support โœ… Native โš ๏ธ Plugin required
File Size Lightweight Heavy
Flexibility High Medium

๐Ÿ“š Next Steps

  1. Start Small - Begin with a simple route and gradually add features
  2. Explore Blueprints - Organize your code into logical modules
  3. Implement Security - Leverage built-in encryption and session management
  4. Scale Up - Use async handlers and proper deployment strategies
  5. Contribute - Help make PieShark even better

Ready to dive in? Start building your next web application with PieShark today! ๐Ÿฆˆโšก

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

piesharkx-0.1.0.tar.gz (447.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

piesharkx-0.1.0-py3-none-any.whl (453.5 kB view details)

Uploaded Python 3

File details

Details for the file piesharkx-0.1.0.tar.gz.

File metadata

  • Download URL: piesharkx-0.1.0.tar.gz
  • Upload date:
  • Size: 447.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.7

File hashes

Hashes for piesharkx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f914bcb39c5973e561661c09f7851a8f870bf39cb26cd1ac119461ac1e74efec
MD5 92998b1524f44dfac8f1be31a1ba4e21
BLAKE2b-256 c08be63ecd8b529f4777eb4dc58349f9221ae32595b720784a4f2236aa7cce81

See more details on using hashes here.

File details

Details for the file piesharkx-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: piesharkx-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 453.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.7

File hashes

Hashes for piesharkx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ca212ac96ff8253a7814dcd891742c67af2ca0c7f6b828cb1ccb8706ee0ce22f
MD5 de9373aa7a735e3331fcabf20b46019d
BLAKE2b-256 dc653dedfffa0922eeca8bcf31342a20a88d99e028b5880c246efd82cdf8f864

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page