Beispiel #1
0
 def get_template(self, name):
     """Return the jinja template object for a given name"""
     try:
         template = env.get_template(name)
     except:
         template = IPythonHandler.get_template(self, name)
     return template
Beispiel #2
0
 def render_template(self, name, **ns):
     try:
         return IPythonHandler.render_template(self, name, **ns)
     except TemplateError:
         return DEFAULT_TEMPLATE.generate(
             name=name, path=self.lab_config.templates_dir
         )
Beispiel #3
0
    def downloads(handler: IPythonHandler):
        file_paths = get_file_paths(handler)
        if file_paths:
            if len(file_paths) == 0:
                handler.set_status(400, f"Files not found.")
                handler.finish()
                return

            if len(file_paths) == 1:
                send_files(handler, file_paths[0],
                           file_paths[0].split('/')[-1])

            else:
                with TemporaryDirectory() as dir_path:
                    tar_gz_file_name = f'{str(uuid.uuid4())}.tar.gz'
                    tar_gz_file_path = os.path.join(dir_path, tar_gz_file_name)
                    with tarfile.open(tar_gz_file_path, mode='w:gz') as tar:
                        for file_path in file_paths:
                            tar.add(
                                file_path,
                                os.path.join('download',
                                             file_path.split('/')[-1]))

                    send_files(handler, tar_gz_file_path, tar_gz_file_name)

        handler.finish()
Beispiel #4
0
    def uploads(handler: IPythonHandler):
        files = handler.request.files
        files_saved = 0

        if len(files) == 0 or 'file' not in files:
            handler.set_status(
                400,
                f"Can't find 'file' or filename is empty. Files received {len(files)}"
            )
        else:

            for f in files['file']:
                if not allowed_file(f.filename):
                    logging.warn(
                        f"Can't store file {f.filename}. Extension not allowed"
                    )
                    continue

                # Save to file
                filename = f.filename
                file_path = os.path.join(UPLOAD_FOLDER_PATH, filename)

                with open(file_path, 'wb') as zf:
                    zf.write(f['body'])

                files_saved += 1

                if filename.endswith('.zip'):
                    with ZipFile(file_path) as zipObj:
                        zipObj.extractall(UPLOAD_FOLDER_PATH)

                elif filename.endswith('.tar.gz'):
                    with tarfile.open(file_path, mode='r:gz') as tar:
                        tar.extractall(UPLOAD_FOLDER_PATH)

                elif filename.endswith('.gz'):
                    with gzip.open(file_path, "rb") as gz, open(
                            file_path.replace('.gz', ''), 'wb') as ff:
                        shutil.copyfileobj(gz, ff)

            handler.set_status(
                200,
                f"Number of files saved: {files_saved}. Number of files sent: {len(files['file'])}"
            )

        handler.finish()
Beispiel #5
0
 def check_origin(self, origin=None):
     return IPythonHandler.check_origin(self, origin)
Beispiel #6
0
    def __init__(self, *args, **kwargs):
        IPythonHandler.__init__(self, *args, **kwargs)
        self.memory_limit = MemoryHandler.get_memory_limit()

        # https://www.tornadoweb.org/en/stable/concurrent.html#tornado.concurrent.run_on_executor
        self.executor = ThreadPoolExecutor(max_workers=10)
Beispiel #7
0
 def initialize(self, *args):
     self.root = ""
     IPythonHandler.initialize(self, *args)
     StaticFileHandler.initialize(self, "", *args)
Beispiel #8
0
 def new_notebook(handler: IPythonHandler, path):
     if not os.path.exists(path):
         logging.info("Creating notebook {}".format(path))
         createNotebook(path)
     handler.redirect('notebooks/' + path)