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.
[Share] Running code from clipboard
-
First of all please, excuse my bad English and code :)
So I was browsing the forums, looking at all sorts of topics. I got tired of copy-pasting the scripts into a new file, every time I wanted to test a script someone wrote. So I (of course) thought it'd be nice if I could just run the script from the app extension, without leaving Safari.
So I wrote this, which kinda works most of the time. (I tried to comment it as best as I could.)
import clipboard import runpy # Every line of the copied script cliplines = clipboard.get().split('\n') # Some scripts contain the 'if __name__ == "__main__":' statement # We can't have that since we need the script to run when it's imported try: cliplines[cliplines.index("if __name__ == '__main__':")] = "if __name__ != '__main__':" except ValueError: pass # @Phuket2 keeps importing editor without using it # Since editor is not supported in the app extension... # We try to remove it and see if it still works for i in range(len(cliplines)): cliplines[i] = (cliplines[i].replace('import editor', '').replace(', editor', '') + '\n').encode() # Writing the new script to the .py file with open('TheScript.py', 'wb') as f: f.writelines(cliplines) # Run the script runpy.run_path('TheScript.py')
It works by writing the text in your clipboard to a .py file and then running it.
I would appreciate I someone helped improve it
(Here's some random code to test it on)
from math import pi from ui import View, TextView # Just a pointless function def changeText(sender, text): sender.text = text v = View() v.bg_color = 'white' v.frame = (0, 0, 400, 100) l = TextView() l.alignment = 1 l.editable = False l.frame = (0, 0, len(str(pi)) * 20, 96) l.font = ('CourierNewPSMT', 32) changeText(l, 'Pi\n' + str(pi)) if __name__ == '__main__': v.present('sheet') l.x = v.width/2 - l.width/2 l.y = v.height/2 - l.height/2 v.add_subview(l)
Edit: What @omz said
-
Wouldn't it be simpler to use
runpy.run_path('TheScript.py')
? That way,from x import *
should also work, and it's less complicated overall.I'd also recommend removing the blanket
except
block at the end. The error message "there's a problem with the code" is usually less helpful than an actual traceback. -
@omz yeah it probably would. I looked up ways to make scripts run other scripts, but none of them worked in pythonista. I just didn't come across runpy.
-
runpy is a module in python standard library and it is not specific to pythonista,
-
FindCodeInForum
Copy hyperlink and than choose the code... it's just another approach... -
@uj_jonas , lol, I see you use my name in vain as they say 😱 You are right though., I do import ui and editor. I have several snippets I use to create some starter code. But now since the addition of themes which are accessed with the editor module, I changed my snippets just to include ui and edititor. Just convince. More often than not I want that import.
-
@Phuket2 I could have left that part out, but didn't since you're such a big part of the forum.
There's not much code to run, if it can't run yours ;)
-