-1

I want to choose the name of the function below a decorator dynamically (during runtime):

@app.server.route('/downloadable/<path:path>')
def the_name_which_I_actually_would_like_to_set_dynamically(path):
    pass

UPDATE:

This is a more specific code sample. The first call of create_endpoint works fine, however I get an error for the second call of create_endpoint

import dash
from flask import Flask

server = Flask(__name__)
app = dash.Dash(server=server)
app.config['suppress_callback_exceptions']=True

def create_endpoint(function_name = "test_function_name"):
    @app.server.route('/downloadable/<path:path>')
    def test_function_name(path):
        pass

create_endpoint()
create_endpoint()

-->

AssertionError: View function mapping is overwriting an existing endpoint function: test_function_name
Oliver Wilken
  • 2,654
  • 1
  • 24
  • 34
  • 3
    Can you share more details about how you're using this? Do you want to change the attributes of the function object for inspection purposes, or do you actually want to assign to a different name? How are you planning to write code that uses those functions if you don't know what they're called? – Patrick Haugh Jan 22 '20 at 19:22
  • Based on [How do I create a variable number of variables?](https://stackoverflow.com/q/1373164/4518341), you might find it easier to put the function in a dict as a value and vary its key. But it depends what you're actually trying to accomplish; this seems like an [XY-problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – wjandrea Jan 22 '20 at 19:28
  • @PatrickHaugh: I added a code-sample to make it clearer – Oliver Wilken Jan 22 '20 at 19:36
  • @wjandrea: sorry I don't get how this helps me to solve my problem – Oliver Wilken Jan 22 '20 at 19:37
  • @PatrickHaugh: is the example clear enough? Or more details required? – Oliver Wilken Jan 22 '20 at 19:52
  • @Oliver Yeah it doesn't help, cause your original code was unclear – wjandrea Jan 22 '20 at 20:07
  • @wjandrea: sorry for that! – Oliver Wilken Jan 22 '20 at 20:12
  • @Oliver No worries! in fact maybe I should have waited for you to clarify before recommending anything – wjandrea Jan 22 '20 at 20:13

1 Answers1

1

You can directly use add_url_rule. The rule itself (the url pattern) need to be unambiguous, so the server know what function to use.

def test_function():
    pass

app.server.add_url_rule("/alpha", "alpha", test_function)
app.server.add_url_rule("/bravo", "bravo", test_function)
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96