Thor
Simple, Powerful web framework for Python
Usage
Table of Contents
Installation
You can install Thor Framework with 3 different ways.
Install with PIP
$ pip install thorFramework
Package: project/thorFramework
Install from Github (Releases)
Go the Relases page, select a releases.
$ wget https://github.com/AsgardIO/Thor/archive/vRELASEVERSION.YOUSELECTED.tar.gz
$ tar -xzvf vRELASEVERSION.YOUSELECTED.tar.gz
$ cd TheDirYouExtractedFile
$ pip install -r requirements.txt
$ python setup.py install
Install from Branches (Dev)
Dev Branch: tree/dev
Master Branch: tree/master
$ git clone -b TheBranchYouChoose https://github.com/AsgardIO/Thor.git
$ cd Thor
$ pip install -r requirements.txt
$ python setup.py install
Quick Start
Hello world application in Thor Framework powered by WSGI
Import Thor
You can import main "thor" class from "thor.thor.webEngine"
from thor.thor.webEngine import thor
If you want to import specific class (such as Thor's template engine "Micrengine") you can import it from "thor.thor".
from thor.thor import tMicrengine
Initialize Thor
You can simply initialize thor by calling it self.
app = thor()
Thor class takes this parameters
- static_folder, takes path, defines static folder's location
- template_folder, takes path, defines template folder's location
- host, takes ip addr, defines serving ip (you dont have to define it here. When you start serving you can set this parameter (by calling run function of thor, thor().run(host=myLovelyHost)
- port, takes port(int), defines serving port (you dont have to define it here. When you start serving you can set this parameter (by calling run function of thor, thor().run(port=myLovelyPort)
Writing Routes, URL Rules (Decorator-based)
Thor gives you 2 different options for routing mechanism (Map based and Decorator based). If you want keep this simple you can use Decorator based routing mechanism.
@app.route(URL)
Route decorator takes this parameters
- url, takes url (without ip:port prefix), defines the route's serving addr
- methods, takes method (v2.0.1 supports GET and POST), defines the witch methods route accept
- **options, other options for WSGI app.
Defining Views (Function Based)
Thor gives you 2 different options for defining views (Class based and function based).
@app.route(URL) # The route
def sayHello():
return "Hello World"
Function Based views takes "req" parameter. It gives everything we know about the request, server, client.
Req returns this parameters
environ
The WSGI environment that the request object uses for data retrival.
shallow
If shallow is True the environment is initialized as shallow object around the environ. Every operation that would modify the environ in any way (such as consuming form data) raises an exception unless the shallow attribute is explicitly set to False. This is useful for middlewares where you don’t want to consume the form data by accident. A shallow request is not populated to the WSGI environment.
url
The reconstructed current URL as IRI
Running Application
You can simply call ".run()" function of "thor" class.
app.run()
".run()" function takes this parameters
- host, takes ip addr, defines serving ip
- port, takes port(int), defines serving port
- **options, other options for WSGI app.
Putting All Together
from thor.thor.webEngine import thor
app = thor()
app.route('/')
def sayHello():
return "Hello World"
app.run()
And test it
batuhan@osmantaskaya î‚° ~ î‚° curl -X GET localhost:1234
Hello World