Amirhossein Omidifard
Amirhossein Omidifard

Reputation: 21

How to assign function argument in flask methods?

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

Upvotes: 1

Views: 359

Answers (1)

Vedant Vasishtha
Vedant Vasishtha

Reputation: 395

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

Upvotes: 1

Related Questions