Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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