PageRenderTime 5902ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/v3/pythontutor.py

https://github.com/ashishraste/OnlinePythonTutor
Python | 92 lines | 41 code | 24 blank | 27 comment | 0 complexity | 61e8d4288c916c730e6f1f13d227c463 MD5 | raw file
  1. # Online Python Tutor
  2. # https://github.com/pgbovine/OnlinePythonTutor/
  3. #
  4. # Copyright (C) 2010-2013 Philip J. Guo (philip@pgbovine.net)
  5. #
  6. # Permission is hereby granted, free of charge, to any person obtaining a
  7. # copy of this software and associated documentation files (the
  8. # "Software"), to deal in the Software without restriction, including
  9. # without limitation the rights to use, copy, modify, merge, publish,
  10. # distribute, sublicense, and/or sell copies of the Software, and to
  11. # permit persons to whom the Software is furnished to do so, subject to
  12. # the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included
  15. # in all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. # TODO: if we want to enable concurrent requests, then make sure this is threadsafe (e.g., no mutable globals)
  25. # then add this string to app.yaml: 'threadsafe: true'
  26. import webapp2
  27. import pg_logger
  28. import json
  29. import jinja2, os
  30. import sys
  31. # TODO: this croaks for some reason ...
  32. TEST_STR = "import os\nos.chdir('/')"
  33. JINJA_ENVIRONMENT = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
  34. class TutorPage(webapp2.RequestHandler):
  35. def get(self):
  36. self.response.headers['Content-Type'] = 'text/html'
  37. template = JINJA_ENVIRONMENT.get_template('visualize.html')
  38. self.response.out.write(template.render())
  39. class IframeEmbedPage(webapp2.RequestHandler):
  40. def get(self):
  41. self.response.headers['Content-Type'] = 'text/html'
  42. template = JINJA_ENVIRONMENT.get_template('iframe-embed.html')
  43. self.response.out.write(template.render())
  44. class LessonPage(webapp2.RequestHandler):
  45. def get(self):
  46. self.response.headers['Content-Type'] = 'text/html'
  47. template = JINJA_ENVIRONMENT.get_template('lesson.html')
  48. self.response.out.write(template.render())
  49. class ExecScript(webapp2.RequestHandler):
  50. def json_finalizer(self, input_code, output_trace):
  51. ret = dict(code=input_code, trace=output_trace)
  52. json_output = json.dumps(ret, indent=None) # use indent=None for most compact repr
  53. self.response.out.write(json_output)
  54. def get(self):
  55. self.response.headers['Content-Type'] = 'application/json'
  56. self.response.headers['Cache-Control'] = 'no-cache'
  57. # convert from string to a Python boolean ...
  58. cumulative_mode = (self.request.get('cumulative_mode') == 'true')
  59. heap_primitives = (self.request.get('heap_primitives') == 'true')
  60. pg_logger.exec_script_str(self.request.get('user_script'),
  61. cumulative_mode,
  62. heap_primitives,
  63. self.json_finalizer)
  64. app = webapp2.WSGIApplication([('/', TutorPage),
  65. ('/iframe-embed.html', IframeEmbedPage),
  66. ('/lesson.html', LessonPage),
  67. ('/exec', ExecScript)],
  68. debug=True)