Use these templates to build your own Flask application in a way that will be supported by SciComp.
Python 3 was released in 2008 and Python 3.x should be used for all new projects. If you have an existing Python 2 codebase, contact SciComp for help with porting it to Python 3.
Use Pipenv to manage the virtual environment for this project.
If pipenv
is not installed, install it.
The first time you clone the repository, create the virtual environment and install the dependencies with this command:
pipenv install
Before each session of working with this app, activate the virtual environment in your current shell with:
pipenv shell
Flask makes it easy to add unit tests to your application. Unit tests give you confidence that your app works the way it's supposed to.
Every route in a Flask app should have at least one unit test.
For most new applications, we recommend SQLAlchemy, which provides an Object-Relational Mapper (ORM) for Python.
There are some cases where using an ORM is overkill and you just need to run some simple queries. In these cases, you can use the DB API module for your RDBMS (MySQL, PostgreSQL, sqlite). When using these low-level modules, you must always write code in such a way as to avoid SQL injection. For example, never do this:
# Never do this -- insecure!
symbol = 'RHAT' # assume this comes from a form or other untrusted source
c.execute("SELECT * FROM stocks WHERE symbol = '%s'" % symbol)
Instead do this:
t = ('RHAT',)
c.execute('SELECT * FROM stocks WHERE symbol=?', t)
PostgreSQL is the main supported RDBMS. You may use sqlite3 just for testing and development(??), but in production you should use PostgreSQL.
For PostgreSQL, use myDB. If your app lives outside the Hutch network, use Amazon RDS for PostgreSQL.
If you need to use a NoSQL database, use MongoDB and the PyMongo module.
If your application is meant to expose a service that other code can consume, use the Flast-RESTful module.
(??)
TODO fill this in
It's recommended that your web app only be accessible via
SSL (the HTTPS protocol). If your web app will use a fredhutch.org
or fhcrc.org
domain, contact SciComp
for help setting this up.
If you'll be using an external domain name, consider using
Let's Encrypt to set up your
SSL certificates.
?? Do we need this section ?? Are flask apps allowed to submit jobs to the cluster ??
We recommend that you port the R code to Python.
(??)
FIXME add to this
FIXME add to this
Store your code in a GitHub repository (ADD MORE HERE)
We strongly recommend using linters
when developing
Python code. Linters such as pylint
, pyflakes
,
and flake8
(which combines the first two) will point
out syntactic, stylistic, logical, and many other issues
with your code. Ideally, code should be free of linting
warnings before it's committed to Git.
The Atom text editor has add-on packages which enable linting right inside your editing window.
Profiling helps you find the parts of your code that run slowly and can be optimized.
Use Travis CI or circleci to automate the testing and deployment of your code.
(should we require CI? we can at least illustrate it by example in this repository)