Package: mango-framework
Mango FrameworkIntroductionMango 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.FeaturesEasy routing configurationRendering HTML templatesServing static filesHandling JSON dataHandling of basic form dataLightweight and minimal dependenciesSuitable for small to medium-sized web applicationsHuman readible code even beginners could modifyIntegrated basic Template engine ShakeHandling of file uploadsSetting custom 404 error pagesHandling static filesInstallationMango can be easily installed via pip:pipinstallmango-frameworkUsageImport the necessary modules and functions from Mango:frommangoimportroute,run,render,send_json,send_file,get_json,save_file,set_404,set_static_url,enable_staticDefine your routes using the @route() decorator:@route('/')defindex():return"Hello, Mango!"Get JSON data:@route('/post')defpost(post):user=get_json(post)returnf"Hello,{user['name']}!"Send JSON data:@route('/send')defsend():returnsend_json({'name':'john'})Send a file for the user to download:@route('/download')defdownload():returnsend_file('image.jpeg')### as attachment@route('/download')defdownload():returnsend_file('image.jpeg',as_attachment=True)Render the HTML to the user (now supports shake without the class):@route('/render')defrender():returnrender('index.html')### New reactive rendering@route('/render')defrender():returnrender('index.html',{'name':'john'})Get form data:@route('/form')defform(form_data):name=form_data.get('name')returnf"Hello,{name}!"Get file upload with data:@route('/get')defupload(form_fields,files):input1=form_fields.get("input1")input2=form_fields.get("input2")file_item1=files[0]iffile_item1.filename:file_path1=save_file(data=file_item1.file.read(),name=file_item1.filename,path='upload')file_item2=files[1]iffile_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 dictreturn"files saved successfully"Change the default 404 Page:set_404("<h1> not here ! </h1>")## or pass an HTML or any file directlyset_404("404.html")Send static files to HTML:<linkrel="stylesheet"type="text/css"href="/static/style.css">Change the default Static URLset_static_url("/images")Default URL is /static. the new link MUST start with / !Enable or disable Static servingenable_static(True)Run the Mango server:run()What's New in 1.2Dynamic Route Configuration via JSONMango 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 ConfigurationDefine Routes in JSON: Create arouting.jsonfile within your project directory. This file will contain all your route definitions in a structured format.Load Routes: Utilize theload_from_jsonfunction at the start of your application to load the routes defined in therouting.jsonfile.Run Your Server: With the routes dynamically loaded, start your Mango server as usual.JSON Configuration ExampleBelow is an example of how to structure yourrouting.jsonto 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 RoutesTo dynamically load the routes from yourrouting.jsonand 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.