app = Flask(__name__)
@app.route("/hello/<username>")
def hello_user(username):
return "Hello {}!".format(username)
import re
route_regex = re.compile(r"^/hello/(.+)$")
match = route_regex.match("/hello/ains")
print match.groups()
/hello/(<?P<username>.+)"
route_regex = re.compile(r'^/hello/(?P<username>.+)$')
match = route_regex.match("/hello/ains")
print match.groupdict()
def build_route_pattern(route):
route_regex = re.sub(r'(<w+>)', r'(?P1.+)', route)
return re.compile("^{}$".format(route_regex))
print build_route_pattern('/hello/<username>')
/hello/<username>
^/hello/(?P<username>.+)$
class NotFlask():
def __init__(self):
self.routes = {}
def route(self, route_str):
def decorator(f):
self.routes[route_str] = f
return f
return decorator
def serve(self, path):
view_function = self.routes.get(path)
if view_function:
return view_function()
else:
raise ValueError('Route "{}"" has not been registered'.format(path))
app = NotFlask()
@app.route("/")
def hello():
return "Hello World!"
class NotFlask():
def __init__(self):
self.routes = []
# Here's our build_route_pattern we made earlier
@staticmethod
def build_route_pattern(route):
route_regex = re.sub(r'(<w+>)', r'(?P1.+)', route)
return re.compile("^{}$".format(route_regex))
def route(self, route_str):
def decorator(f):
# Instead of inserting into a dictionary,
# We'll append the tuple to our route list
route_pattern = self.build_route_pattern(route_str)
self.routes.append((route_pattern, f))
return f
return decorator
def get_route_match(path):
for route_pattern, view_function in self.routes:
m = route_pattern.match(path)
if m:
return m.groupdict(), view_function
return None
def hello_user(username):
return "Hello {}!".format(username)
>>> hello_user("ains")
Hello ains!
>>> hello_user(username="ains") Hello ains!
>>> kwargs = {"username": "ains"}
>>> hello_user(**kwargs)
Hello ains!
class NotFlask():
def __init__(self):
self.routes = []
@staticmethod
def build_route_pattern(route):
route_regex = re.sub(r'(<w+>)', r'(?P1.+)', route)
return re.compile("^{}$".format(route_regex))
def route(self, route_str):
def decorator(f):
route_pattern = self.build_route_pattern(route_str)
self.routes.append((route_pattern, f))
return f
return decorator
def get_route_match(self, path):
for route_pattern, view_function in self.routes:
m = route_pattern.match(path)
if m:
return m.groupdict(), view_function
return None
def serve(self, path):
route_match = self.get_route_match(path)
if route_match:
kwargs, view_function = route_match
return view_function(**kwargs)
else:
raise ValueError('Route "{}"" has not been registered'.format(path))
app = NotFlask()
@app.route("/hello/")
def hello_user(username):
return "Hello {}!".format(username)
print app.serve("/hello/ains")
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-3 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.yuanmawang.com) 版权所有