forked from msysbio/bacterial_growth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request msysbio#6 from AndrewRadev/flask-ui-5
Flask UI: Part 5, steps 1 and 2 of the upload process
- Loading branch information
Showing
42 changed files
with
7,294 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#! /bin/sh | ||
|
||
python flask_app/main.py | ||
|
||
# If there's a syntax error, the flask server will crash, let's rerun it on | ||
# change: | ||
|
||
inotifywait -m -e modify flask_app/** | | ||
while read file_path file_event file_name; do | ||
echo "DEBUG: $file_path $file_event $file_name" | ||
python flask_app/main.py | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from flask_wtf import FlaskForm | ||
from wtforms import SelectField, SelectMultipleField, StringField, FormField, FieldList | ||
from wtforms.validators import DataRequired, Optional | ||
|
||
|
||
class NewStrainsForm(FlaskForm): | ||
class Meta: | ||
csrf = False | ||
|
||
name = StringField('name', validators=[DataRequired()]) | ||
description = StringField('description', validators=[Optional()]) | ||
species = SelectField('species') | ||
|
||
|
||
class UploadStep2Form(FlaskForm): | ||
strains = SelectMultipleField('strains') | ||
new_strains = FieldList(FormField(NewStrainsForm), min_entries=0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from flask_wtf import FlaskForm | ||
from wtforms import SelectField, SelectMultipleField, IntegerField | ||
from wtforms.validators import DataRequired | ||
|
||
|
||
class UploadStep3Form(FlaskForm): | ||
vessel_type = SelectField('vessel_type', choices=[ | ||
('bottles', "Bottles"), | ||
('agar_plates', "Agar plates"), | ||
('well_plates', "Well plates"), | ||
('mini_react', "mini bioreactors"), | ||
], validators=DataRequired()) | ||
|
||
bottle_count = IntegerField('bottle_count') | ||
plate_count = IntegerField('plate_count') | ||
column_count = IntegerField('column_count') | ||
row_count = IntegerField('row_count') | ||
|
||
timepoint_count = IntegerField('timepoint_count', validators=[DataRequired()]) | ||
|
||
technique_type = SelectField('technique_type', choices=[ | ||
('od', "Optical Density"), | ||
('plates', "Plate Counts"), | ||
('plates_ps', "Plate Counts (per species)"), | ||
('fc', "Flow Cytometry"), | ||
('fc_ps', "Flow Cytometry (per species)"), | ||
('rna', "16S rRNA-seq"), | ||
], validators=DataRequired()) | ||
|
||
technique_type = SelectMultipleField('metabolites') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
from uuid import uuid4 | ||
|
||
import sqlalchemy as sql | ||
|
||
|
||
class Submission: | ||
def __init__(self, data, current_step=None, db_conn=None): | ||
self.step = current_step or 1 | ||
self.db_conn = db_conn | ||
|
||
# Step 1: | ||
self.type = data.get('type', None) | ||
self.project_uuid = data.get('project_uuid', None) | ||
self.study_uuid = data.get('study_uuid', None) | ||
|
||
self.project = data.get('project', {'name': None, 'description': None}) | ||
|
||
# Step 2: | ||
self.strains = data.get('strains', []) | ||
self.new_strains = data.get('new_strains', []) | ||
|
||
def update_project(self, data): | ||
self.type = data['submission_type'] | ||
|
||
if self.type == 'new_project': | ||
self.project_uuid = uuid4() | ||
self.study_uuid = uuid4() | ||
|
||
self.project = { | ||
'name': data['project_name'], | ||
'description': data['project_description'], | ||
} | ||
elif self.type == 'new_study': | ||
self.project = _get_project_data(self.db_conn, data['project_uuid']) | ||
if self.project: | ||
self.project_uuid = data['project_uuid'] | ||
|
||
self.study_uuid = uuid4() | ||
elif self.type == 'update_study': | ||
self.project = _get_project_data(self.db_conn, data['project_uuid']) | ||
if self.project: | ||
self.project_uuid = data['project_uuid'] | ||
|
||
self.study_uuid = data['study_uuid'] | ||
else: | ||
raise KeyError("Unknown self type: {}".format(self.submission_type)) | ||
|
||
def update_strains(self, data): | ||
self.strains = data['strains'] | ||
self.new_strains = data['new_strains'] | ||
|
||
def update_study_design(self, data): | ||
pass | ||
|
||
def fetch_strains(self): | ||
if len(self.strains) == 0: | ||
return [] | ||
|
||
# TODO (2024-09-22) Figure out a sensible way to bind ids | ||
|
||
query = f""" | ||
SELECT | ||
tax_id AS id, | ||
tax_names AS name | ||
FROM Taxa | ||
WHERE tax_id IN ({','.join(self.strains)}) | ||
""" | ||
result = self.db_conn.execute(sql.text(query)).all() | ||
|
||
return [(entry.id, entry.name) for entry in result] | ||
|
||
def fetch_new_strains(self): | ||
if len(self.new_strains) == 0: | ||
return [] | ||
|
||
new_strains = sorted(self.new_strains, key=lambda s: int(s['species'])) | ||
species_ids = [s['species'] for s in new_strains] | ||
|
||
query = f""" | ||
SELECT tax_names | ||
FROM Taxa | ||
WHERE tax_id IN ({','.join(species_ids)}) | ||
ORDER BY tax_id | ||
""" | ||
species_names = self.db_conn.execute(sql.text(query)).scalars() | ||
|
||
for strain, name in zip(new_strains, species_names): | ||
strain['species_name'] = name | ||
|
||
return new_strains | ||
|
||
def _asdict(self): | ||
return { | ||
'type': self.type, | ||
'project_uuid': self.project_uuid, | ||
'study_uuid': self.study_uuid, | ||
'project': self.project, | ||
'strains': self.strains, | ||
'new_strains': self.new_strains, | ||
} | ||
|
||
|
||
def _get_project_data(db_conn, uuid): | ||
query = """ | ||
SELECT | ||
projectName AS name, | ||
projectDescription AS description | ||
FROM Project | ||
WHERE projectUniqueID = :uuid | ||
""" | ||
result = db_conn.execute(sql.text(query), {'uuid': uuid}).one_or_none() | ||
|
||
if result is None: | ||
return None | ||
else: | ||
return result._asdict() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,71 @@ | ||
from flask import render_template | ||
from flask import render_template, session, request, redirect, url_for | ||
|
||
from flask_app.db import get_connection | ||
from flask_app.models.submission import Submission | ||
from flask_app.forms.upload_step2_form import UploadStep2Form | ||
from flask_app.forms.upload_step3_form import UploadStep3Form | ||
|
||
def upload_index_page(): | ||
return render_template("pages/upload.html") | ||
|
||
def upload_step1_page(): | ||
with get_connection() as conn: | ||
submission = Submission(session.get('submission', {}), current_step=1, db_conn=conn) | ||
error = None | ||
|
||
if request.method == 'POST': | ||
submission.update_project(request.form) | ||
|
||
if submission.project: | ||
session['submission'] = submission._asdict() | ||
return redirect(url_for('upload_step2_page')) | ||
else: | ||
error = "No project found with this UUID" | ||
|
||
return render_template( | ||
"pages/upload/index.html", | ||
submission=submission, | ||
error=error | ||
) | ||
|
||
|
||
def upload_step2_page(): | ||
with get_connection() as conn: | ||
submission = Submission(session.get('submission', {}), current_step=2, db_conn=conn) | ||
form = UploadStep2Form(request.form) | ||
|
||
if request.method == 'POST': | ||
submission.update_strains(form.data) | ||
session['submission'] = submission._asdict() | ||
|
||
return redirect(url_for('upload_step3_page')) | ||
|
||
return render_template( | ||
"pages/upload/index.html", | ||
submission=submission, | ||
) | ||
|
||
|
||
def upload_step3_page(): | ||
with get_connection() as conn: | ||
submission = Submission(session.get('submission', {}), current_step=3, db_conn=conn) | ||
form = UploadStep3Form(request.form) | ||
|
||
if request.method == 'POST': | ||
submission.update_study_design(form.data) | ||
session['submission'] = submission._asdict() | ||
|
||
return redirect(url_for('upload_step3_page')) | ||
|
||
return render_template( | ||
"pages/upload/index.html", | ||
submission=submission, | ||
) | ||
|
||
|
||
def upload_step4_page(): | ||
with get_connection() as conn: | ||
submission = Submission(session.get('submission', {}), current_step=4, db_conn=conn) | ||
|
||
return render_template( | ||
"pages/upload/index.html", | ||
submission=submission, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.