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.


    Possible bug in editor module, when used via button callback

    Pythonista
    3
    5
    2896
    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.
    • galtenberg
      galtenberg last edited by

      I'm seeing a difference and possible bug in the behavior of the editor module. (using version 1.5)

      I want to read a file's contents. It works when running a basic script, but not when triggered by a button press.

      Spent awhile on this, I believe this is the simplest path to reproduce:

      1. Create two scripts, one called "ReadMe.py" and one called "RunMe.py"
      2. Put some junk text in ReadMe
      3. Open RunMe and add a UI (calling the necessary load_view and present)
      4. Create a method called open_script, using the code below
      5. Create a button
      6. Set the action of the button to be open_script
      7. Run the UI, click the button

      Code:

      from console import hud_alert
      
      def open_script(sender):
        editor.open_file('ReadMe.py')
        time.sleep(0.5) # give time for file to be loaded
        hud_alert(editor.get_path())
        hud_alert(editor.get_text())
      

      You'll see the path and first line of RunMe rather than ReadMe. Although when you close the UI, you'll be on the ReadMe script.

      But if you run the logic of open_script just on its own, not through the UI, everything works as expected.

      Please advise a workaround or correct my misunderstanding?

      Thank you for Pythonista!

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

        Try:

        @ui.in_background
        def open_script(sender):
        
        1 Reply Last reply Reply Quote 0
        • galtenberg
          galtenberg last edited by

          Brilliant, that worked, thank you!

          To perform a replace in that background file, I still have to leave the time.sleep() in (with a sleep less than 0.3 typically nothing happens)... Is there a better way to do that as well?

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

            To perform a replace in that background file, I still have to leave the time.sleep() in (with a sleep less than 0.3 typically nothing happens)... Is there a better way to do that as well?

            There isn't a better way right now. The editor opens files after a brief delay, which is to prevent iOS from potentially killing the app if it takes too long to launch... I'll probably change editor.open_file in the future to return after the file has been completely loaded, but for now you'll have to live with the sleep workaround.

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

              Makes complete sense, thank you.

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