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
-
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:
- Create two scripts, one called "ReadMe.py" and one called "RunMe.py"
- Put some junk text in ReadMe
- Open RunMe and add a UI (calling the necessary load_view and present)
- Create a method called open_script, using the code below
- Create a button
- Set the action of the button to be open_script
- 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!
-
Try:
@ui.in_background def open_script(sender):
-
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?
-
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 thesleep
workaround. -
Makes complete sense, thank you.