1

I have some functions that should send on URL by flask. At first I want to assign a argument to function by get from a psth. My code:

app= Flask(__name__)
@app.route('/')
def hello (x):
   x=data
   return f"{x}"

data =pd.read_csv("Path\\file.csv")

Unfortunately I receive this Error in my url:

TypeError: hello() missing 1 required positional argument: 'x'

This function works out of flask method routinely. How can I assign argument (x) to the function??? I'm stressful because I should present a report to my supervisor. Thanks a lot

  • Does this answer your question? [How can I pass arguments into redirect(url\_for()) of Flask?](https://stackoverflow.com/questions/26954122/how-can-i-pass-arguments-into-redirecturl-for-of-flask) – Chen A. Jan 11 '21 at 06:40

1 Answers1

1

You can define argument in url pattern.

Note: No. of arguments defined in url and function argument should be same.

app= Flask(__name__)
@app.route('/<arg__name>')
def hello (arg_name):
   arg_name=data
   return f"{arg_name}"

data =pd.read_csv("Path\\file.csv")

If you want to restrict the datatype you can define it in following way. For e.g to restrict for "int":

app= Flask(__name__)  
@app.route('/<int: id>')
def hello (id):
       id=data
       return f"{id}"
    
data =pd.read_csv("Path\\file.csv")

For more information, click here