Greetings, I'm having an issue with passing a Para...
# ask-community
f
Greetings, I'm having an issue with passing a Parameter from the command line to my flow, if I'm correct on the server the Parameter can be passed in the UI, but for testing I need to pass in sys.argv[2]. here's the relevant code:
def register__flow():
    
with Flow("FlowName", schedule=None, ) as flow:
        
file_name = Parameter(name='file_name',default=None)
        
params = get_params()
        
p = get_file_blob(
                        
file_name=f"notebooks/{file_name}",
                        
con_string=params["uploads_blob"],
                        
container="uploads"
        
)
        
flow.add_edge(file_name,params)
        
flow.add_edge(params, p)
    
if len(sys.argv) > 1 and sys.argv[1] == "register":
        
flow.register(project_name="project_x")
    
elif len(sys.argv) > 1 and sys.argv[1] == "test" and len(sys.argv[2]) > 1:
        
flow.run(parameters={'file_name':sys.argv[2]})
    
else:
        
print("Please use a quoted string of the file name...")
if __name__ == "__main__":
    
register__flow()
Thanks
k
Hi @Frederick Thomas! What error do you get?
f
I'm getting a file not found error from
Azure a print statement in the get_file_blob function shows: string = notebooks/<Parameter: file_name>
Which makes total since, I just not sure if I'm passing the parameter correctly.
k
Ok I’ll try this on my end and get back to you
f
Thanks!!! This is driving me nuts...
k
It works for me. Here is my sample flow:
Copy code
import prefect
from prefect import Flow, task, Parameter
import sys

@task
def test(x):
    logger = prefect.context.get("logger")
    <http://logger.info|logger.info>(x)
    return

with Flow("test") as flow:
    param = Parameter("param", default= sys.argv[2])
    test(param)
flow.run()
command was
python flow.py test1 test2
and test2 gets logged.
f
Hmmm, give me a few minutes
I did as you suggested and the Parameter is still not getting passed:
def register__flow():
    
with Flow("FlowName", schedule=None, ) as flow:
        
file_name = Parameter(name='file_name',default=sys.argv[2])
        
params = get_params()
        
p = get_file_blob(
                        
file_name=f"notebooks/{file_name}",
                        
con_string=params["uploads_blob"],
                        
container="uploads"
        
)
        
flow.add_edge(file_name,params)
        
flow.add_edge(params, p)
    
if len(sys.argv) > 1 and sys.argv[1] == "register":
        
flow.register(project_name="project_x")
    
elif len(sys.argv) > 1 and sys.argv[1] == "test" and len(sys.argv[2]) > 1:
        
flow.run()
    
else:
        
print("Please use a quoted string of the file name...")
if __name__ == "__main__":
    
register__flow()
k
Is
get_file_blob
there a task?
Ah sorry I missed it on the first pass. You should use
filename = task(lambda p: f"notebooks/{p}")(file_name)
inside
get_file_blob
. This is because
file_name
has no value yet when you do that. Parameters get values during runtime and this string is given a value at build time. You need some helper task to generate the file name during runtime as task execution is deferred.
Updated the code snippet
f
@Kevin Kho It is now working as expected:
Copy code
p = get_file_blob(
                        file_name = task(lambda p: f"notebooks/{p}")(file_name),
                        con_string=params["fileuploader_blob"],
                        container="fu-uploads"
        )
We're currently using version 0.14.12, is there any documentation that is would emphasize this requirement? Otherwise, thanks!
k
This is a core Prefect thing that
tasks
are used to defer execution. Maybe this documentation on tasks will help you