|
| 1 | +# Copyright (c) Jupyter Development Team. |
| 2 | +# Distributed under the terms of the Modified BSD License. |
| 3 | + |
| 4 | +import json |
| 5 | + |
| 6 | +from tornado import web |
| 7 | + |
| 8 | +from jupyter_server.auth.decorator import authorized |
| 9 | +from jupyter_server.base.handlers import APIHandler |
| 10 | +from jupyter_server.utils import url_path_join |
| 11 | + |
| 12 | + |
| 13 | +class OutputsAPIHandler(APIHandler): |
| 14 | + """An outputs service API handler.""" |
| 15 | + |
| 16 | + auth_resource = "outputs" |
| 17 | + |
| 18 | + @property |
| 19 | + def outputs(self): |
| 20 | + return self.settings["outputs_manager"] |
| 21 | + |
| 22 | + @web.authenticated |
| 23 | + @authorized |
| 24 | + async def get(self, file_id=None, cell_id=None, output_index=None): |
| 25 | + try: |
| 26 | + output = self.outputs.get(file_id, cell_id, output_index) |
| 27 | + except (FileNotFoundError, KeyError): |
| 28 | + self.set_status(404) |
| 29 | + self.finish({"error": "Output not found."}) |
| 30 | + else: |
| 31 | + self.set_status(200) |
| 32 | + self.set_header("Content-Type", "application/json") |
| 33 | + self.write(output) |
| 34 | + |
| 35 | + |
| 36 | +class StreamAPIHandler(APIHandler): |
| 37 | + """An outputs service API handler.""" |
| 38 | + |
| 39 | + auth_resource = "outputs" |
| 40 | + |
| 41 | + @property |
| 42 | + def outputs(self): |
| 43 | + return self.settings["outputs_manager"] |
| 44 | + |
| 45 | + @web.authenticated |
| 46 | + @authorized |
| 47 | + async def get(self, file_id=None, cell_id=None): |
| 48 | + try: |
| 49 | + output = self.outputs.get_stream(file_id, cell_id) |
| 50 | + except (FileNotFoundError, KeyError): |
| 51 | + self.set_status(404) |
| 52 | + self.finish({"error": "Stream output not found."}) |
| 53 | + else: |
| 54 | + # self.set_header("Content-Type", "text/plain; charset=uft-8") |
| 55 | + self.set_header("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0") |
| 56 | + self.set_header("Pragma", "no-cache") |
| 57 | + self.set_header("Expires", "0") |
| 58 | + self.set_status(200) |
| 59 | + self.write(output) |
| 60 | + self.finish(set_content_type="text/plain; charset=utf-8") |
| 61 | + |
| 62 | + |
| 63 | +# ----------------------------------------------------------------------------- |
| 64 | +# URL to handler mappings |
| 65 | +# ----------------------------------------------------------------------------- |
| 66 | + |
| 67 | +# Strict UUID regex (matches standard 8-4-4-4-12 UUIDs) |
| 68 | +_uuid_regex = r"[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}" |
| 69 | + |
| 70 | +_file_id_regex = rf"(?P<file_id>{_uuid_regex})" |
| 71 | +_cell_id_regex = rf"(?P<cell_id>{_uuid_regex})" |
| 72 | + |
| 73 | +# non-negative integers |
| 74 | +_output_index_regex = r"(?P<output_index>0|[1-9]\d*)" |
| 75 | + |
| 76 | +handlers = [ |
| 77 | + (rf"/api/outputs/{_file_id_regex}/{_cell_id_regex}/{_output_index_regex}.output", OutputsAPIHandler), |
| 78 | + (rf"/api/outputs/{_file_id_regex}/{_cell_id_regex}/stream", StreamAPIHandler), |
| 79 | +] |
| 80 | + |
| 81 | +# def setup_handlers(web_app): |
| 82 | +# """Setup the handlers for the outputs service.""" |
| 83 | + |
| 84 | +# handlers = [ |
| 85 | +# (rf"/api/outputs/{_file_id_regex}/{_cell_id_regex}/{_output_index_regex}.output", OutputsAPIHandler), |
| 86 | +# (rf"/api/outputs/{_file_id_regex}/{_cell_id_regex}/stream", StreamAPIHandler), |
| 87 | +# ] |
| 88 | + |
| 89 | +# base_url = web_app.settings["base_url"] |
| 90 | +# new_handlers = [] |
| 91 | +# for handler in handlers: |
| 92 | +# pattern = url_path_join(base_url, handler[0]) |
| 93 | +# new_handler = (pattern, *handler[1:]) |
| 94 | +# new_handlers.append(new_handler) |
| 95 | + |
| 96 | +# # Add the handler for all hosts |
| 97 | +# web_app.add_handlers(".*$", new_handlers) |
0 commit comments