24
loading...
This website collects cookies to deliver better user experience
ExpressJs
? Do you like the auto completion features of Vscode
while using typing based language or framework? Do you want to get all of above mentioned features while using a Python based framework called Flask
?munch
module to provide the attribute-style access very similar to the Javascript. Below I have tried to mention some of the examples to demonstrate the features of Flaske better.python3 -m pip install flaske
git clone https://github.com/marktennyson/flaske.git && cd flaske/
python3 setup.py install
from flaske import Flask
app = Flask(__name__)
@app.get("/")
def index(req, res):
return res.json(req.header)
from flaske import Flask
app = Flask(__name__)
@app.get("/")
async def index(req, res):
return res.json(req.header)
from flaske import Flask
from flaske.typing import Request, Response
app = Flask(__name__)
@app.get("/")
def index(req:Request, res:Response):
return res.json(req.header)
json
property of the request class to access the data.get_json
method to provide the data. @app.post("/send-json")
def send_json(req, res):
name = req.json.name
email = req.json.email
return res.json(name=name, email=email)
args
property to provide the data. @app.get("/get-query")
def get_query(req, res):
name=req.query.name
email = req.query.email
return res.send(dict(name=name, email=email))
form
property to provide the data. @app.get("/get-form-data")
def get_form_data(req, res):
name=req.body.name
email = req.body.email
return res.send(dict(name=name, email=email))
header
property to provide the data. @app.get("/get-form-data")
def get_form_data(req, res):
return res.send(req.header)
@app.route("/set-status")
def set_statuser(req, res):
return res.set_status(404).send("your requested page is not found.")
@app.route('/flash')
def flasher(req, res):
return res.flash("this is the flash message").end()
@app.route("/send")
def sender(req, res):
return res.send("hello world")
#or
return res.send("<h1>hello world</h1>")
#or
return res.set_status(404).send("not found")
@app.route("/json")
def jsoner(req, res):
return res.json(name="aniket sarkar")
#or
return res.json({'name': 'aniket sarkar'})
#or
return res.json([1,2,3,4])
@app.route("/end")
def ender(req, res):
return res.end()
#or
return res.end(404) # to raise a 404 error.
@app.route('/render')
def renderer(req, res):
context=dict(name="Aniket Sarkar", planet="Pluto")
return res.render("index.html", context)
#or
return res.render("index.html", name="Aniket Sarkar", planet="Pluto")
@app.post("/login")
def login(req, res):
#if login success
return res.redirect("/dashboard")
@app.route("/get")
def getter(req, res):
print (res.get("Content-Type"))
return res.end()
@app.route("/header-seter")
def header_setter(req, res):
res.set('Content-Type', 'application/json')
#or
res.set({'Content-Type':'application/json'})
return res.end()
@app.route("/set-mime")
def mimer(req, res):
res.type('application/json')
#or
res.type(".html")
#or
res.type("json")
attachments
.ATTACHMENTS_FOLDER
.@app.route('/attachments')
def attach(req, res):
filename = req.query.filename
return res.attachment(file_name)
Flask.wrappers.Request
development
branch.python setup.py install
command.flaske
module on your virtual environment.[email protected]
.