
from flask import Blueprint, render_template
import os
from flask import *
import secrets


def make_token():
	return secrets.token_urlsafe(4)

read_qual = Blueprint('read_qual', __name__,
	 template_folder=os.path.join('templates'))

@read_qual.route('/genomics')
def render_cheminfo_pca_input():
	return render_template('genomics.html')

@read_qual.route('/genomics/input_process', methods=['POST'])
def get_sra_file():
	file = request.files['file']
	if not file.filename.endswith('.fastq'):
		return render_template('genomics.html', error="not a fastq file")
	else:
		session['TOKEN'] = make_token()
	if not os.path.exists(os.path.join(current_app.root_path, 'genomics', session.get("TOKEN"))):
		os.makedirs(os.path.join(current_app.root_path,
											  'genomics', session.get("TOKEN")))
		 
		file.save(os.path.join(current_app.root_path,
										'genomics', session.get("TOKEN"), file.filename))
		 
		from genomics.run_nb import run_notebook
				 
		result = run_notebook(os.path.join(current_app.root_path, 'genomics', session.get(
					 "TOKEN"), file.filename), session.get("TOKEN"))
		if result is True:
			return render_template(f"{session.get('TOKEN')}_output.html")
