Skip to content

Instantly share code, notes, and snippets.

@briarfox
Last active June 14, 2018 03:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briarfox/90841da64d0d6e961dbf to your computer and use it in GitHub Desktop.
Save briarfox/90841da64d0d6e961dbf to your computer and use it in GitHub Desktop.
Simulates python -m module_name in Pythonista iOS
'''
python_run.py
Simulates a console call to >python -m module|file [args]
Used for running standard library python modules such as:
SimpleHTTPServer, unittest and .py files.
usage:
module_name [args]
path/python_file.py [args]
Example:
SimpleHTTPServer 8090
'''
import runpy
import os
import editor
import sys
import ui
@ui.in_background
def callback(sender):
sys.argv[0] = editor.get_path()
args = sender.text.split(' ')
filename = args.pop(0)
view.close()
os.chdir(os.path.dirname(editor.get_path()))
if os.path.isfile(filename):
try:
sys.argv[1:] = args
runpy.run_path(filename, run_name='__main__')
except Exception, e:
print 'Error: '+ str(e)
else:
try:
sys.argv[1:] = args
runpy.run_module(str(filename), run_name='__main__')
except ImportError, e:
print 'ImportError: '+str(e)
view = ui.View()
view.name = 'Python'
view.frame = (0, 0, 210, 40)
textbox = ui.TextField()
textbox.frame = (5, 5, 200, 30)
textbox.action = callback
view.add_subview(textbox)
view.present('popover')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment