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)
async def siofu_start(sid, data): try: path = generate_uuid() filename = secure_filepath(data.get('name')) tmp_fs = Filesystem('tmpfs://') async with socketio.session(sid) as sess: sess['file_%d' % (data.get('id'))] = dict( data, path=path, name=filename, bytesLoaded=0, tmp_fs=tmp_fs, fh=tmp_fs.open(path, 'wb'), ) await socketio.emit( 'siofu_ready', { 'id': data.get('id'), 'name': None, }, room=sid, ) except Exception as e: logger.error(traceback.format_exc()) await socketio.emit('siofu_error', str(e), room=sid)
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)
def get_fields(): ''' Helper to get/cache fields even if we're on a different thread ''' global _fields if not _fields or current_app.config['DEBUG']: fs = Filesystem(current_app.config['CWD']) with fs.open(current_app.config['IPYNB'], 'r') as fr: env = get_jinja2_env(config=current_app.config) nbtemplate = nb_from_ipynb_io(fr) _fields = render_nbtemplate_json_from_nbtemplate(env, nbtemplate) return _fields
def get_index(): mimetype = request.accept_mimetypes.best_match([ 'text/html', 'application/json', 'application/vnd.jupyter', ], 'text/html') fs = Filesystem(current_app.config['CWD']) if mimetype in {'text/html'}: with fs.open(current_app.config['IPYNB'], 'r') as fr: env = get_jinja2_env(config=current_app.config) nbtemplate = nb_from_ipynb_io(fr) return render_form_from_nbtemplate(env, nbtemplate) elif mimetype in {'application/json'}: with fs.open(current_app.config['IPYNB'], 'r') as fr: env = get_jinja2_env(config=current_app.config) nbtemplate = nb_from_ipynb_io(fr) return jsonify(render_nbtemplate_json_from_nbtemplate(env, nbtemplate)) elif mimetype in {'application/vnd.jupyter'}: return send_file(fs.open(current_app.config['IPYNB'], 'rb'), attachment_filename=current_app.config['IPYNB'], mimetype=mimetype) else: abort(404)
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)
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)
def get_ipynb_hash(): global _ipynb_hash if not _ipynb_hash or current_app.config['DEBUG']: fs = Filesystem(current_app.config['CWD']) _ipynb_hash = sha1sum_io(fs.open(current_app.config['IPYNB'], 'rb')) return _ipynb_hash