Skip to content

Instantly share code, notes, and snippets.

@fhats
Created December 11, 2011 03:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fhats/1458068 to your computer and use it in GitHub Desktop.
Save fhats/1458068 to your computer and use it in GitHub Desktop.
Subclass of Flask to automatically store routes in the view handler's docstrings
class FlaskWithRouteDocs(Flask):
"""A subclass of Flask to allow using :route: and :methods: in docstrings, a la Sphinx."""
def route(self, rule, **options):
def decorator(f):
route_dec = super(FlaskWithRouteDocs, self).route(rule, **options)
methods_str = ','.join(options.get('methods', ['GET']))
if f.__doc__ is not None and ":route:" in f.__doc__:
f.__doc__ = f.__doc__.replace(":route:", rule)
else:
f.__doc__ = "%s\n\n **Route:** ``%s``" % (f.__doc__, rule)
if f.__doc__ is not None and ":methods:" in f.__doc__:
f.__doc__ = f.__doc__.replace(":methods:", methods_str)
else:
f.__doc__ = "%s\n\n **Methods:** ``%s``" % (f.__doc__, methods_str)
return route_dec(f)
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment