A Webapp for ML computations.
Go to the folder where you want to start creating your project.
Create conda environment, e.g. via
conda create -n djangoenv python=3.8
Then activate it with
conda activate djangoenv
Install the requirements in the nevironment via
pip install -r requirements.txt
Finally, set the up the new project with
django-admin startproject mysite
In the newly created folder, you will find the following files:
settings.py
a file containing the generic settings of the projecturls.py
a file containing the urls of the apps of the projectwsgi.py
a file for the WSGI deployment
Go inside the outer gtdainterface
folder and then run:
python manage.py runserver 8080
Just type
redis-server
Go inside the outer gtdainterface
folder and then run:
python manage.py rqworker default
Create a .env
file with the following secret variables:
export SENDGRID_API_KEY = 'xxx'
export RQ_API_TOKEN = 'xxx'
export SECRET_KEY = 'xxx'
export DROPBOX_OAUTH2_TOKEN = 'xxx'
export DATABASE_URL = 'xxx'
In particular:
- SENDGRID_API_KEY is for sending emails with sendgrid
- RQ_API_TOKEN is for RQ to contact the Redis server
- SECRET_KEY this is the django app secret key
- DROPBOX_OAUTH2_TOKEN tis s to configure Dropbox storage API
- DATABASE_URL this is for the online PostgreSQL database
Go inside the outer mysite
folder, and then choose the name -- polls
in this case -- and run
python manage.py startapp polls
views.py
this file is to define the classes or functions associated to the views (basically, the get response). Each function must return thehttpResponse
admin.py
is the file to improve the admin viewmodels.py
is teh file with the definition of the classes associated to the database structurestests.py
is for the unit and integration testsurls.py
specifies all the urls associated to the viewsapps.py
a file with the app configurations
To create tables in the databse for auth, session, contenttype, messages, staticfiles write
python manage.py migrate
First you need to install psycopg2
:
pip install psycopg2
and then
If you create a new DB model, then add its migration via
python manage.py makemigrations polls
If you are using postgreSQL, then use
python manage.py sqlmigrate polls 0001
To actually apply the model tables in your database, use
python manage.py migrate
To open up the Python shell use
python manage.py shell
Just type
python manage.py createsuperuser
E.G. admin, [email protected], admin
To run the tests, that you can write in the polls/tests.py
file, write
python manage.py test polls
Forr ML ocmputation, it is better to have a Redis server that enqueues the jobs and execute them in the background.
Install Redis
and Python rq
:
brew install redis
pip install django-rq
To star the redis server, run
redis-server
Once you are done, follow the instructions of django-rq. Then run the workers with:
python manage.py rqworker default
You can also add the high
and low
queues
You can use signals that are triggered when a certain event happens. For exammple, every time an entry is saved in a table of the database.
To do so, initalise the connections in the apps.py
file and then create a new signals.py
module with the receiver functions.
Visit the app site now!
Need to install postgres Need to download te heroku cli:
brew install heroku/brew/heroku
Login with
heroku login
Then create and deploy the app: first move to the root folder
heroku create
git commit -am "commit files and modifications"
git push heroku main
heroku ps:scale web=1
heroku open
To scale down heroku app processes:
heroku ps:scale web=0
To clear the build, for example when you modify the Procfile
heroku buildpacks:clear
and the re-coimmit
git commit --allow-empty -m "Adjust buildpacks on Heroku"
git push heroku main
To migrate the database, type
heroku run python manage.py migrate
Type
heroku run python manage.py createsuperuser
Simply run
python manage.py collectstatic
Simply:
heroku run bash
Simply
heroku run python manage.py shell
Set secrets using heroku config
heroku config:set VAR = 'xxxx'
heroku addons:create redistogo:nano
Add this to the Procfile:
worker: python gtdainterface/manage.py rqworker high
And add a worker to heroku:
heroku ps:scale worker=1
Activate PostgreSQL database
heroku addons
and check the heroku config
to find the db url. To make the migration
heroku run python manage.py migrate
Write in the terminal:
heroku logs --tail