python - Cherrypy expose multiple items to same function -
i have large number of names, should link url correspond names. know generating function each element in list , decorate @cherrypy.expose
, wonder if possible link items instead function function
without opening same site each item. (something generating appropriate function on fly, lambda function, , passing appropriate argument?)
import cherrypy class test(object): @cherrypy.expose def index(self): html1 = '''<html> <head></head> <body> <ul style="list-style: none;">''' html2 = """</ul> </body> </html>""" html3 = "" name in names: html3 += "<li><a href='%s'>%s</a></li>" %(name.lower(), name) return html1 + html3 + html2 @cherrypy.expose def function(self, name=''): print(name) return name
you can use special method default
:
import cherrypy cp class app: @cp.expose def index(self): return "this index" @cp.expose def default(self, name): return "default name: %s" % name @cp.expose def branch(self): return "specific branch" cp.quickstart(app())
you can try urls: /, /branch, /any_other_name. can defined in more generic term method definition this:
@cp.expose def default(self, *args, **kwargs): return "default args: %s , kwargs: %s" % (args, kwargs)
in way can accept pretty anything. map url parts args
list , query string kwargs
.
Comments
Post a Comment