Beispiel #1
0
def data_files(path):
    if path.endswith('/'):
        mimetype = request.accept_mimetypes.best_match([
            'text/html',
            'application/json',
            'application/vnd.jupyter',
        ], 'text/html')
        if mimetype == 'text/html':
            fs = Filesystem(current_app.config['CWD'])
            env = get_jinja2_env(config=current_app.config)
            return env.get_template('landing.j2').render(_nb=os.path.basename(
                current_app.config['IPYNB']), )
        else:
            data_fs = Filesystem(
                Filesystem.join(current_app.config['DATA_DIR'], 'output'))
            path += current_app.config['IPYNB']
            if data_fs.exists(path):
                return send_file(data_fs.open(path, 'rb'),
                                 attachment_filename=os.path.basename(path))
    else:
        data_fs = Filesystem(
            Filesystem.join(current_app.config['DATA_DIR'], 'output'))
        if data_fs.exists(path):
            return send_file(data_fs.open(path, 'rb'),
                             attachment_filename=os.path.basename(path))
    abort(404)
Beispiel #2
0
def static(filename):
  static = Filesystem(current_app.config['STATIC_DIR'])
  if static.exists(filename):
    return send_file(static.open(filename, 'rb'), attachment_filename=filename)
  #
  try:
    return send_from_directory(get_appyter_directory(f"profiles/{current_app.config['PROFILE']}/static"), filename=filename)
  except NotFound:
    return send_from_directory(get_appyter_directory('profiles/default/static'), filename=filename)
Beispiel #3
0
def prepare_results(data):
    results_hash = sha1sum_dict(dict(ipynb=get_ipynb_hash(), data=data))
    data_fs = Filesystem(current_app.config['DATA_DIR'])
    results_path = Filesystem.join('output', results_hash)
    if not data_fs.exists(
            Filesystem.join(results_path, current_app.config['IPYNB'])):
        # prepare files to be linked and update field to use filename
        file_fields = {
            field['args']['name']
            for field in get_fields() if field['field'] == 'FileField'
        }
        links = []
        files = {}
        for file_field in file_fields:
            if fdata := data.get(file_field):
                content_hash, filename = fdata.split('/', maxsplit=1)
                content_hash = sanitize_sha1sum(content_hash)
                filename = secure_filepath(filename)
                links.append((Filesystem.join('input', content_hash),
                              Filesystem.join(results_path, filename)))
                files[filename] = filename
                data[file_field] = filename
        # construct notebook
        env = get_jinja2_env(config=current_app.config,
                             context=data,
                             session=results_hash)
        fs = Filesystem(current_app.config['CWD'])
        with fs.open(current_app.config['IPYNB'], 'r') as fr:
            nbtemplate = nb_from_ipynb_io(fr)
        # in case of constraint failures, we'll fail here
        nb = render_nb_from_nbtemplate(env, nbtemplate, files=files)
        # actually link all input files into output directory
        for src, dest in links:
            data_fs.link(src, dest)
        # write notebook
        nbfile = Filesystem.join(results_path,
                                 os.path.basename(current_app.config['IPYNB']))
        with data_fs.open(nbfile, 'w') as fw:
            nb_to_ipynb_io(nb, fw)
Beispiel #4
0
def favicon():
  static = Filesystem(current_app.config['STATIC_DIR'])
  if static.exists('favicon.ico'):
    return send_file(static.open('favicon.ico', 'rb'), attachment_filename='favicon.ico')
  abort(404)