Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Pastebin helper for Online Python Tutor
#!/usr/bin/env python
import sys
import urllib
import webbrowser
def get_opt_url(s, from_bpython=True):
""" Use json to post to github. """
if from_bpython:
def fix(text):
return ('\n'.join(line[4:] if line[:4] in ('... ', '>>> ') else '### '+line
for line in text.split('\n')).encode('utf8'))
s = fix(s)
url = 'http://www.pythontutor.com/visualize.html'
s = s[:-1] if s.endswith('\x0A') else s
#Doing encoding manually instead of urllib.encode because order
data = [
('mode', 'display'),
('cumulative', 'false'),
('heapPrimitives', 'false'),
('drawParentPointers', 'false'),
('textReferences', 'false'),
('showOnlyOutputs', 'false'),
('py', '2'),
]
req = url + '#code=' + urllib.quote_plus(s) + '&' + urllib.urlencode(data)
return req
def view_in_opt(s):
webbrowser.open_new_tab(get_opt_url(s))
if __name__ == "__main__":
s = sys.stdin.read()
view_in_opt(s)
print get_opt_url(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment