Home > Enterprise >  How to take dynamic input from flask and pass it to other function?
How to take dynamic input from flask and pass it to other function?

Time:01-02

How can I take input from url in flask from the parameter?

@app.route('/<id>')
def give_id(id):
    
    return id

I need to take the above Id from input and pass it to other function without again needing to write "id"

def some_function():
    variable1 = vid_stream.get_hls_url(give_id("I need to again pass that Id here"))
  

How can I directly use the id from give_id(id) function and feed it into vid_stream.get_hls_url function?

Posting complete demo code, In case someone needs to run locally.

from flask import Flask
app = Flask(__name__)

@app.route('/<id>')
def give_id(id):
        
   return id

def some_function():
   variable1 = vid_stream.get_hls_url(give_id("I need to again pass that Id here"))
   print(variable1)
some_function()

if __name__ == '__main__':
    app.run(host='0.0.0.0',port=8080)

CodePudding user response:

This should work. id is reserved keyword so i have renamed it you can call the function by passing the value. You can call some_function from you give_id view directly.

from flask import Flask
app = Flask(__name__)

def some_function(p_id):
   variable1 = vid_stream.get_hls_url(give_id("I need to again pass that Id here"))
   print(variable1)
   return variable1

@app.route('/<id>')
def give_id(p_id):
   output = some_function(p_id) 
   return p_id



if __name__ == '__main__':
    app.run(host='0.0.0.0',port=8080)
  • Related