Python Forum
Passing flags to python script, through a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Passing flags to python script, through a function
#1
I have a python script, that i am able to call it from the command line, and i pass the parameters to it, through flags on the terminal. For example:

python my_program.py --parameter1 "0.4" --parameter2 "none"
Now, i want to be able to call that script from inside flask.
I can easily wrap the python script in a function, but what i do now know, is how will i be able to pass the parameters to it?

Keep in mind, that the python script handles parameters with

parser.add_argument()
How will i be able to pass the flags, as function parameters (or something like that)?
Reply
#2
You can use subprocess.run()
import subprocess
import sys
subprocess.run([sys.executable, "my_program.py", "--parameter1", "0.4", "--parameter2", "none"])
Reply
#3
Thank you very much!
Doing this however, will create a new process in the process list? Am i correct?

Thus, if you 'spawn' it five times, five new processes will be created. Is this correct?
Reply
#4
Yes, each call of suprocess.run() creates a new process.
Reply
#5
If creating another process is unnecessary, then exposing a function that you call from the Flask app is the right idea. Have it take as an argument the list of string arguments that you then pass to the parser. Presumably, you're using argparse.ArgumentParser, whose parse_args method does let you do that (see the docs). So, in the script, you'd just pass sys.argv to that function and in your Flask app, yeah, you'd just pass a list of strings containing the values you wanted. It's just good old dependency injection.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 345 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 728 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,185 Jun-29-2023, 11:57 AM
Last Post: gologica
  passing dictionary to the function mark588 2 965 Dec-19-2022, 07:28 PM
Last Post: deanhystad
  Regex - Pass Flags as a function argument? muzikman 6 3,578 Sep-06-2021, 03:43 PM
Last Post: muzikman
  string function doesn't work in script ClockPillow 3 2,386 Jul-13-2021, 02:47 PM
Last Post: deanhystad
  passing php variable to python file jerald 1 2,678 Jul-07-2021, 11:46 AM
Last Post: Larz60+
  Confused with 'flags' tester_V 10 4,891 Apr-12-2021, 03:03 AM
Last Post: tester_V
  Passing argument from top-level function to embedded function JaneTan 2 2,236 Oct-15-2020, 03:50 PM
Last Post: deanhystad
  Passing List of Objects in Command Line Python usman 7 3,162 Sep-27-2020, 03:45 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020