Skip to content

Instantly share code, notes, and snippets.

saurabh hirani saurabh-hirani

Block or report user

Report or block saurabh-hirani

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View flask_1
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
return "Hello, World!"
if __name__ == '__main__':
app.run(debug = True)
View decorator-ex1
#!/usr/bin/env python
def decorator(func):
def wrapper_around_func():
print "Decorator calling func"
func()
print "Decorator called func"
return wrapper_around_func
@decorator
View decorator-ex2
#!/usr/bin/env python
import inspect
from functools import wraps
def get_line_number():
# inspect the current frame, go back 2 frames and get the line number.
return inspect.currentframe().f_back.f_back.f_lineno
View decorator-ex3.py
#!/usr/bin/env python
import inspect
from functools import wraps
def get_line_number():
# inspect the current frame, go back 2 frames and get the line number.
return inspect.currentframe().f_back.f_back.f_lineno
View decorator-ex4.py
#!/usr/bin/env python
import inspect
from functools import wraps
def get_line_number():
# inspect the current frame, go back 2 frames and get the line number.
return inspect.currentframe().f_back.f_back.f_lineno
View decorator-ex5.py
#!/usr/bin/env python
def decorator_factory():
def decorator(func):
def decorator_wrapper():
print "Decorator calling func"
func()
print "Decorator called func"
return decorator_wrapper
return decorator
View flask-route-src.py
def route(self, rule, **options):
def decorator(f):
endpoint = options.pop('endpoint', None)
self.add_url_rule(rule, endpoint, f, **options)
return f
return decorator
View flask-register-deco-min.py
#!/usr/bin/env python
import sys
class MyApp():
def __init__(self):
self.func_map = {}
def register(self, name):
def func_wrapper(func):
View callbacks-ex1.py
#!/usr/bin/env python
def func1(arg):
print "calling func1 with arg " + str(arg)
def func2(arg):
print "calling func2 with arg " + str(arg)
callbacks_dict = { 'func1': func1, 'func2': func2 }
callbacks_dict['func1'](1)
View add_url_rule_signature
def add_url_rule(self, rule, endpoint=None, view_func=None, **options):
"""Connects a URL rule. Works exactly like the :meth:`route`
decorator. If a view_func is provided it will be registered with the
endpoint.
Basically this example::
@app.route('/')
def index():
pass
You can’t perform that action at this time.