Skip to main content

A lightweight and highly customizable Python framework for building web applications

Project description

Mango Framework

License Mango Icon

Introduction

Mango is a lightweight Python framework for building web applications. It provides a simple and intuitive way to handle routing, render HTML templates, and serve files. With Mango, you can quickly set up a web server and define routes to handle different HTTP requests. It is made to be accessible and highly modifiable even by beginners to learn and eventually move on to more mature frameworks such as Flask or Bottle. You only need python3 to run Mango and nothing else.

Features

  • Easy routing configuration
  • Rendering HTML templates
  • Serving static files
  • Handling JSON data
  • Handling of basic form data
  • Lightweight and minimal dependencies
  • Suitable for small to medium-sized web applications
  • Human readible code even beginners could modify
  • Integrated basic Template engine Shake
  • Handling of file uploads
  • Setting custom 404 error pages
  • Handling static files

Installation

Mango can be easily installed via pip:

pip install mango-framework

Usage

  1. Import the necessary modules and functions from Mango:
from mango import route, run, render, send_json, send_file, get_json, save_file, set_404, set_static_url, enable_static
  1. Define your routes using the @route() decorator:
@route('/')
def index():
    return "Hello, Mango!"
  1. Get JSON data:
@route('/post')
def post(post):
    user = get_json(post)
    return f"Hello, {user['name']}!"
  1. Send JSON data:
@route('/send')
def send():
    return send_json({'name':'john'})
  1. Send a file for the user to download:
@route('/download')
def download():
    return send_file('image.jpeg')

### as attachment

@route('/download')
def download():
    return send_file('image.jpeg', as_attachment=True)
  1. Render the HTML to the user (now supports shake without the class):
@route('/render')
def render():
    return render('index.html')

### New reactive rendering

@route('/render')
def render():
    return render('index.html',{'name':'john'})
  1. Get form data:
@route('/form')
def form(form_data):
    name = form_data.get('name')
    return f"Hello, {name}!"
  1. Get file upload with data:
@route('/get')
def upload(form_fields, files):
    input1 = form_fields.get("input1")  
    input2 = form_fields.get("input2")

    file_item1 = files[0]  
    if file_item1.filename:  
        file_path1 = save_file(data=file_item1.file.read(), name=file_item1.filename, path='upload')

    file_item2 = files[1]
    if file_item2.filename:
        file_path2 = save_file(data=file_item2.file.read(), name=file_item2.filename, path='upload')

        #returns files in a list, forms in a dict

    return "files saved successfully"
  1. Change the default 404 Page:
set_404("<h1> not here ! </h1>")

## or pass an HTML or any file directly

set_404("404.html")
  1. Send static files to HTML:
<link rel="stylesheet" type="text/css" href="/static/style.css">
  1. Change the default Static URL
set_static_url("/images")

Default URL is /static. the new link MUST start with / !

  1. Enable or disable Static serving
enable_static(True)
  1. Run the Mango server:
run()

What's New in 1.2

Dynamic Route Configuration via JSON

Mango 1.2 introduces the ability to configure routes dynamically using a JSON file. This feature significantly simplifies route management by allowing developers to define routes, handlers, and their responses in a JSON format, making your web application easily configurable and adaptable without the need to directly modify the Python code, the included ORM has been removed in favor of third party more robust alternatives such as tinyDB.

How to Use JSON Route Configuration

  1. Define Routes in JSON: Create a routing.json file within your project directory. This file will contain all your route definitions in a structured format.

  2. Load Routes: Utilize the load_from_json function at the start of your application to load the routes defined in the routing.json file.

  3. Run Your Server: With the routes dynamically loaded, start your Mango server as usual.

JSON Configuration Example

Below is an example of how to structure your routing.json to define various routes:

{
    "GET": {
        "/": {
            "handler": "index",
            "return": {
                "type": "template",
                "name": "index.html",
                "context": {
                    "name": "test",
                    "time": "11AM"
                }
            }
        },
        "/json": {
            "handler": "send_json",
            "return": {
                "type": "json",
                "data": {
                    "name": "test",
                    "time": "11AM"
                }
            }
        },
        "/redirect": {
            "handler": "redirect_func",
            "return": {
                "type": "redirect",
                "url": "/json"
            }
        },
        "/plain": {
            "handler": "send_plain",
            "return": {
                "type": "plain",
                "data": "Hello World"
            }
        },
        "/file": {
            "handler": "send_file_func",
            "return": {
                "type": "file",
                "path": "a.jpg",
                "attachment": false
            }
        },
        "/data": {
            "handler": "send_data",
            "return": {
                "type": "data",
                "data": "Hello World"
            }
        }
    }
}
Loading and Running with JSON Routes

To dynamically load the routes from your routing.json and start the server, include the following code in your application:

data = open("routing.json").read()
json_data_routing = json.loads(data)
load_from_json(json_data_routing)
run()

This enhancement to Mango makes setting up and modifying your server simpler than ever before, promoting rapid development and easier management of web applications through external JSON configurations.

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

mango-framework-1.2.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

mango_framework-1.2-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file mango-framework-1.2.tar.gz.

File metadata

  • Download URL: mango-framework-1.2.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for mango-framework-1.2.tar.gz
Algorithm Hash digest
SHA256 a3bebf539ab1edc3c4aabcfd6d760099552c07b8b7f0dbde856ea409828da42d
MD5 0dc130887b28cd1ab5fabe5582ac928c
BLAKE2b-256 88e9edfcb64855896e35d3185efb9ead6cfc4509359728c3fa99e4c292fbebbd

See more details on using hashes here.

File details

Details for the file mango_framework-1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for mango_framework-1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fe328eee92b24c4b881600db1dbdcdeea0b77f8a4915e35fd3e881e70320c937
MD5 82c6acb5873a4ea282fc41cd359832c5
BLAKE2b-256 2cb1a060aefe497366b145d4a542679ec3171d8c2e34b5f2a47d552835662b84

See more details on using hashes here.

Supported by

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