开发者社区> 问答> 正文

python web framework —扩展模块的动态路由

我正在探索python的Bottle Web框架的使用。Web应用程序必须是可扩展的。也就是说,我将拥有一个类似于“ some / path / extensions /”的文件夹,将来的开发人员应该可以在其中放置此应用程序的扩展名。因此,例如:如果扩展名为“ treelayout”(具有自己的html文件,js文件等,则这些文件将放置在... / ... / extensions / treelayout /文件夹中。我的目标是拥有类似于以下内容的网址路由器:

@GET("/some/path/extensions/:module/:func/:params")
def callmodule(module, func, params):
    #D = dictionary made out of variable number of params.
    #Then call module.func(D)  <== How can I do this given that module and func are strings?

例如,有人可以调用“ h ++ p://.../treelayout/create/maxnodes/100/fanout/10/depth/3”,这将调用新的扩展名treelayout.create({maxnodes:100,fanout :10,depth:3})。

知道是否可以使用bottle或任何其他python框架吗?

展开
收起
祖安文状元 2020-02-22 15:24:49 547 0
1 条回答
写回答
取消 提交回答
  • Python语言可以将字符串变量映射到已加载的模块和方法。

    import sys 
    
    @GET("/some/path/extensions/:module/:func/:params")
    def callmodule(module, func, params):
        D = dict(params)
        # Pull module instance from systems stack
        moduleInstance = sys.modules[module]
        # Grab method from module
        functionInstance = getattr(moduleInstance,func)
        # Pass "D" variable to function instance
        functionInstance(D)
    
    

    在以下问题的答案中可以找到其他出色的例子

    2020-02-22 15:25:03
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Web应用系统性能优化 立即下载
高性能Web架构之缓存体系 立即下载
PWA:移动Web的现在与未来 立即下载