omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Simple UI to run script2 + args

    Pythonista
    5
    23
    14672
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • iApeiron
      iApeiron last edited by

      Thank you. This is a start. But I also need some way to append user input as arguments for script2.
      Essentially, a GUI wrapper for a pre-existing commandline .py

      1 Reply Last reply Reply Quote 0
      • omz
        omz last edited by

        You could just modify sys.argv in the first script, then use runpy.run_path to run the second.

        iApeiron 2 Replies Last reply Reply Quote 1
        • iApeiron
          iApeiron last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • iApeiron
            iApeiron @omz last edited by

            @omz Thank you. I think I have a start now, thanks to coming across this ex13-raw_input.py

            1 Reply Last reply Reply Quote 0
            • iApeiron
              iApeiron @omz last edited by iApeiron

              @omz I've got this working based on the ex13 script

              from sys import argv
              
              script2 = argv
              
              first_name = raw_input("What is your first name? ")
              
              middle_name = raw_input("What is your middle name? ")
              
              last_name = raw_input("What is your last name? ")
              
              print "Your full name is %s %s %s." % (first_name, middle_name, last_name)
              

              But I have not got runpy to cooperate.
              And this doesn't use the 'raw_input' which I need.

              Do you have a solution? Thank you for your help

              1 Reply Last reply Reply Quote 0
              • ccc
                ccc last edited by ccc

                import sys
                import runpy
                
                try:  # https://docs.python.org/3/whatsnew/3.0.html#builtins
                    raw_input          # Python 2
                except NameError:
                    raw_input = input  # Python 3
                
                script2 = sys.argv[1] if sys.argv[1:] else 'script2.py'
                first_name = raw_input("What is your first name? ").strip() or 'Donald'
                middle_name = raw_input("What is your middle name? ").strip() or 'J.'
                last_name = raw_input("What is your last name? ").strip() or 'Trump'
                fmt = "%s: Your full name is %s %s %s."
                print(fmt % (sys.argv[0].split('/')[-1], first_name, middle_name, last_name))
                runpy.run_path(script2, init_globals={'first_name': first_name,
                                                      'middle_name': middle_name,
                                                      'last_name': last_name})
                
                
                """script2.py
                import sys
                fmt = "{}: Your full name is {first_name} {middle_name} {last_name}."
                print(fmt.format(sys.argv[0], **globals()))
                """
                
                iApeiron 1 Reply Last reply Reply Quote 0
                • iApeiron
                  iApeiron @ccc last edited by

                  @ccc Thank you for the thoughtful answer!

                  I think this discussion has strayed away from my original intention.

                  To rephrase my original intention:
                  A have a command line Python script which would be used as follows "python main.py arg1 arg2 arg" and then proceeds to do and print some predefined functions.
                  Now, I want use a wrapper script to run main.py with the args input via a prompt, and print what main.py is doing.

                  Thank you for help!

                  1 Reply Last reply Reply Quote 0
                  • ccc
                    ccc last edited by ccc

                    import sys
                    import runpy
                    
                    try:  # https://docs.python.org/3/whatsnew/3.0.html#builtins
                        raw_input          # Python 2
                    except NameError:
                        raw_input = input  # Python 3
                    
                    script2 = sys.argv[1] if sys.argv[1:] else 'script2.py'
                    first_name = raw_input("What is your first name? ").strip() or 'Donald'
                    middle_name = raw_input("What is your middle name? ").strip() or 'J.'
                    last_name = raw_input("What is your last name? ").strip() or 'Trump'
                    fmt = "%s: Your full name is %s %s %s."
                    print(fmt % (sys.argv[0].split('/')[-1], first_name, middle_name, last_name))
                    sys.argv = ['script2.py', first_name, middle_name, last_name]
                    with open(sys.argv[0]) as in_file:
                        exec(in_file.read())  # exec() is a security hole a mile wide and 10 miles deep!!
                    
                    """script2.py
                    import sys
                    print("{}: Your full name is {} {} {}.".format(*sys.argv))
                    """
                    
                    iApeiron 1 Reply Last reply Reply Quote 0
                    • ccc
                      ccc last edited by

                      Python 3 fix also merged into upstream... https://github.com/mwarkentin/Learn-Python-The-Hard-Way/pull/2/files

                      1 Reply Last reply Reply Quote 1
                      • zipit
                        zipit last edited by

                        Uh, I didn't know that python had a short hand syntax for conditional assignments evaluating forTrue. That is nice 👍🏻.

                        1 Reply Last reply Reply Quote 0
                        • JonB
                          JonB last edited by

                          rich,
                          Are you aware that you long-press the play button for a script, it lets you define arguments? Just checking, not everyone realizes this.

                          another option would be to use a dialogs popup rather than raw_input

                          import dialogs
                          fields=[{'title':'First Name','type':'text'},
                                   {'title':'Last Name','type':'text'},
                                   {'title':'Age','type':'number'}]
                          response=dialogs.form_dialog('What is your info?',fields)
                          
                          iApeiron 1 Reply Last reply Reply Quote 1
                          • iApeiron
                            iApeiron @JonB last edited by

                            @JonB Thanks. I really like this dialogue box! If I can get the responses to act as args to control my second script, then I have it made!

                            1 Reply Last reply Reply Quote 0
                            • iApeiron
                              iApeiron @ccc last edited by

                              This post is deleted!
                              1 Reply Last reply Reply Quote 0
                              • ccc
                                ccc last edited by ccc

                                Put that text at the end into a file called script2.py ;-). Then run the rest of the code in a separate file that I called script1.py.

                                iApeiron 1 Reply Last reply Reply Quote 0
                                • iApeiron
                                  iApeiron @ccc last edited by

                                  @ccc Thanks. I still get the traceback error "AttributeError: 'tuple' object has no attribute 'remove'"

                                  1 Reply Last reply Reply Quote 0
                                  • ccc
                                    ccc last edited by ccc

                                    Try changing this line:

                                    sys.argv = ['script2.py', first_name, middle_name, last_name]

                                    To have square brackets [] instead of parentheses ().

                                    https://github.com/cclauss/runpy_hack

                                    iApeiron 2 Replies Last reply Reply Quote 0
                                    • iApeiron
                                      iApeiron @ccc last edited by

                                      This post is deleted!
                                      1 Reply Last reply Reply Quote 0
                                      • iApeiron
                                        iApeiron @ccc last edited by

                                        @ccc Thank you! This works as expected!
                                        The first two args are necessary, the third arg is supposed to be optional. In its current form, (with Trump removed of course,) leaving the third arg blank causes script2 error (citing an invalid arg).
                                        Do you know how to leave an arg as optional?

                                        1 Reply Last reply Reply Quote 0
                                        • ccc
                                          ccc last edited by ccc

                                          The part about 'Trump' being removed made my day! Thanks for that.

                                          # in script2.py, select one of the following and rename it to main()
                                          
                                          def default_arg_main(script_name, first_name, middle_name, last_name=''):
                                              last_name = last_name or 'Bonehead'
                                              fmt = "{}: Your full name is {} {} {}."
                                              print(fmt.format(script_name, first_name, middle_name, last_name))
                                          
                                          def if_main(*args):
                                              if len(args) == 4:
                                                  fmt = "{}: Your full name is {} {} {}."
                                              else:
                                                  fmt = "{}: Your full name is {} {}."
                                              print(fmt.format(*args))
                                          
                                          def join_main(*args):
                                              print("{}: Your full name is {}.".format(args[0], ' '.join(args[1:])))
                                          
                                          def list_main(*args):
                                              fmt = ('Zero sys.argv is not possible!',
                                                     'Usage: {} first_name, [middle_name, [last_name]]',
                                                     "{}: Your full name is {}.",
                                                     "{}: Your full name is {} {}.",
                                                     "{}: Your full name is {} {} {}.")[len(args)]
                                              print(fmt.format(*args))
                                          
                                          # and then...
                                          print('=' * 44)
                                          main(*sys.argv)
                                          
                                          iApeiron 1 Reply Last reply Reply Quote 0
                                          • iApeiron
                                            iApeiron last edited by iApeiron

                                            Clearly, you are a Python master!

                                            1 Reply Last reply Reply Quote 1
                                            • First post
                                              Last post
                                            Powered by NodeBB Forums | Contributors