handlers.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. """Tornado handlers for frontend config storage."""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import json
  5. import os
  6. import io
  7. import errno
  8. from tornado import web
  9. from ipython_genutils.py3compat import PY3
  10. from ...base.handlers import APIHandler
  11. class ConfigHandler(APIHandler):
  12. @web.authenticated
  13. def get(self, section_name):
  14. self.set_header("Content-Type", 'application/json')
  15. self.finish(json.dumps(self.config_manager.get(section_name)))
  16. @web.authenticated
  17. def put(self, section_name):
  18. data = self.get_json_body() # Will raise 400 if content is not valid JSON
  19. self.config_manager.set(section_name, data)
  20. self.set_status(204)
  21. @web.authenticated
  22. def patch(self, section_name):
  23. new_data = self.get_json_body()
  24. section = self.config_manager.update(section_name, new_data)
  25. self.finish(json.dumps(section))
  26. # URL to handler mappings
  27. section_name_regex = r"(?P<section_name>\w+)"
  28. default_handlers = [
  29. (r"/api/config/%s" % section_name_regex, ConfigHandler),
  30. ]