ckdanny
3/1/2020 - 3:52 AM

Flask Routing mini function

[This is testing] Flask Routing mini function #flask

"""

key, no slash required:
1. route # default GET only
2. (route, methods) # for multiple methods, must be in tuple/set, never use list

!! for param in route, just use it as flask

value:
1. function
2. dict -> branch route

"""

router_map = {
    'api': {
        'v1': {
            ('chartbokehrender', 'POST'): plotting,
            'chart_types': chart_types,
            'palette_list': palette_list,
            ('ddd', 'POST'): ddd,
            ('direct_plot', 'POST'): direct_plotting,
        },
        '': index
    }
}

def map_routing_core(mapping: dict, prefix: str = ''):
    for k, v in mapping.items():
        if isinstance(v, dict):
            map_routing_core(v, '{}/{}'.format(prefix, k))
        elif isinstance(k, (tuple, set)):
            app.route('{}/{}'.format(prefix, k[0]), methods=list(k[1]) if not isinstance(k[1], str) else [k[1]])(v)
        else:
            app.route('{}/{}'.format(prefix, k))(v)


map_routing_core(router_map)