Skip to content

Home > Science > Django setup with rdkit for chemoinformatics studies

Django setup with rdkit for chemoinformatics studies

How to deploy rapidly RdKit using a django installation

Friday 24 June 2016, by Stéphane Téletchéa

Although Rdkit and Django installation and deployment are well documented independently, it may become rapidly difficult to a newcomer to get a simple rdkit + django implementation working using a real apache backend. This article will detail how they can be implemented and used for a small demonstration application.

Django setup

We will first create our django sample application using virtualenv and pip.

virtualenv django1.8
source django1.8/bin/activate
pip install django==1.8.13

Now that the minimum requirements is available, it is time to start a new project called chemoinformaticsDemo.

django-admin startproject chemoinformaticsDemo

This will install django files and a subdirectory called again chemoinformaticsDemo where the projects settings will have to be updated. For now we can keep it as is.

At this point we only need some dirs that will be filled in later.

cd chemoinformaticsDemo
mkdir static
mkdir {{chemoinformaticsDemo}}/templates

There is now the default infrastructure for the django project (without rdkit calls, see below). In order to get a proper django setup, we need to initalize the database for django models, mostly for authentication management at this step.

python makemigrations
python migrate

It you are really impatient, you can already start your application in the developper mode using:

python runserver

To see the result you can fire up your browser and point to the location


To get something more specific, you can add a template in chemoinformaticsDemo/templates, for instance called base.html.


<!doctype html>
<html lang="en">
   <meta  charset="utf-8">
This is the starting page of the project.
   <div id="content">
       {% block content %}{% endblock content %}

This page will be displayed via the urls mapping in django, so you have to specify a simple and, as exemplified after.


from django.conf.urls import patterns, include, url
from django.conf import settings

from django.contrib import admin

urlpatterns = (
   url(r'^$','chemoinformaticsDemo.views.home', name='home'),
   url(r'^admin/', include(,

The corresponding will be called from this url redirection.


from django.shortcuts import render
from django.conf import settings

def home(request):
    return render(request, 'home.html')

If you refresh the previously opened http page, you should now get a small message welcoming you:

This is the starting page of the project.

It is now time to add some content to the project, and especially to link rdkit with django.

RdKit installation

This installation was performed on Ubuntu 14.04.4 LTS 64bits.

First download the latest rdkit tarball. ( RDKit_2016_03_1.tgz )

Decompress the archive, enter the rdkit directory and create a build directory, then use default options, except for the rdkit installation location:

cd /opt
tar -zxvf RDKit_2016_03_1.tgz
cd rdkit-Release_2016_03_1
mkdir build
cd build
sudo cmake  ..
sudo make install

You will need sudo / root rights to write to the /opt directory.

It is important to have rdkit paths available system-wide so Apache will also be able to find it properly. The easiest way is to indicate to the system where it should search for rdkit libraries. This is done by adding a file in the linker directory, in /etc/ We can create a simple file called rdkit.conf, and update the dynamic loader cache to have rdkit included properly. In short:

sudo echo "/opt/rdkit-Release_2016_03_1/lib" > /etc/
sudo ldconfig

If you want to be sure the library search now contains the RdKit path, just grep for it.

grep -i rdkit /etc/
Binary file /etc/ matches

You also need that rdkit is available for python programs out of you django environment.

Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/opt/RDkit_2016_03_1')
>>> from rdkit import Chem
>>> quit()

Please note that the /lib directory is not present in the sys.path.append command because python bindings are present at the RDkit root.

Testing rdkit inside django

Now that everything is in place, we can test a simple but functionnal application. First we create a dedicated application called rdkitDemo (and not simply rdkit otherwise there will be namespace collisions).

python startapp rdkitDemo
mkdir rdkitDemo/templates

You need to edit the chemoinformaticsDemo/ to add to "INSTALLED_APPS" rdkitDemo (using any text editor).

Now django has to take into account this new application, so migrate existing tables.

python makemigrations
python migrate

Up-to-now since no modifications were made to the models (and therefore to the tables) there should be no error messages and no migrations detected.
To enrich you django project, we will add new urls to take into account rdkitDemo and add a sample html file for showing a simple demonstration of rkdit.

update to chemoinformaticsDemo/

from django.conf.urls import patterns, include, url
from django.conf import settings

from django.contrib import admin

urlpatterns = (
   url(r'^$','essai.views.home', name='home'),
   url(r'^admin/', include(,
   {{url(r'^rdkit/', include('rdkitDemo.urls')),}}

The corresponding entry in the new application involves its own file.


from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
   url(r'^demo', 'rdkitDemo.views.simple_example', name='rdkit_simple_example'),

We can now set up a small demonstration of what can be accomplished with rdkit, for instance computing the 2D coordinates of beta galactose from its SMILES description in pubchem, and optimize its geometry in 3D. The generated molecule will be transformed into a 2D plot stored on the disk.

To store the PNG file, add a subdirectory to the static directory seen above.

mkdir static/demo
chmod 777 static/demo

- Important Note: the chmod will be used later, this is not mandatory for now and should also NOT be done on a production server...

Now that everything is in place, we can add the necessary files in rdkitDemo.


from django.shortcuts import render
from django.conf import settings

from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit import DataStructs
from rdkit.Chem.Fingerprints import FingerprintMols
from rdkit.Chem import Draw

from PIL import Image
# Create your views here.

def simple_example(request):
   m = Chem.MolFromSmiles(smiles_data) # Code SMILES issu de pubchem
   # data=Chem.MolToMolBlock(m) # Unused, not pretty to display (yet)
   # 3D conversion and optimization
   AllChem.MMFFOptimizeMolecule(m2) # more complex, better?
   # AllChem.UFFOptimizeMolecule(m) # faster, less stronger :-)
   toto=FingerprintMols.FingerprintMol(m) # not used
   return render(request,'demo.html', {'data':smiles_data})

To display the result of the view, a new template is created, based on the base.html already available.


{% extends "base.html" %}

{% load static %}

{% block title %}Simple rdkit Demo{% endblock title %}

{% block content %}

Welcome to this demo project for the molecule described by {{ data }}.<br />

<img src="{% static 'demo/test.png' %}" alt="Image générée" />

<footer style="text-align:right">
<a href="{% url 'home' %}">Back</a>

{% endblock content %}

If everything worked perfectly until here, you can start again your django project in developper mode, pay attention to update your file to extend the system path for rdkit. Pay also attention to spaces above if you copy/paste the code since python uses spaces (or abs) to indent the code and determine logical blocks.

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
   os.environ.setdefault("DJANGO_SETTINGS_MODULE", "essai.settings")

   from import execute_from_command_line


You should now get a working rdkit + django installation in the developper mode for django.

(django1.8)python runserver

- Reminder: we are still in the virtualenvironment created at the beginning. If you get errors about PIL not found, just add it using pip, and do the same for any missing dependencies.

pip install Pillow

Only one step is needed to migrate this small application to a real server available for the community.

Migration to apache

If everything is is place, you can now configure apache to use your virtual environment and rkdit. Assuming the virtual environment was stored in /home/stephane/example, one needs to write the following apache configuration file.


<VirtualHost *:80>

   ServerName localhost
   ServerAlias localhost

   # Very important, see
   WSGIApplicationGroup %{GLOBAL}


   WSGIDaemonProcess essaiRdkit python-path=/home/stephane/example/chemoinformaticsDemo:/opt/RDkit_2016_03_1/:/home/stephane/example/chemoinformaticsDemo/django1.8/lib/python2.7/site-packages display-name=essaiRdkit
   WSGIProcessGroup essaiRdkit
   WSGIScriptAlias /essai '/home/stephane/example/chemoinformaticsDemo/chemoinformaticsDemo//'

   <Directory /home/stephane/example/chemoinformaticsDemo/>
       WSGIProcessGroup essaiRdkit
           Options +ExecCGI -MultiViews +FollowSymLinks
           AllowOverride None
           Require all granted

   Alias /static /home/stephane/example/chemoinformaticsDemo/static/

   <Location /static/>
       WSGIProcessGroup essaiRdkit
       Options -Indexes +FollowSymLinks
       Require all granted

   # Pour séparer les bons logs de l'ivraie

   ErrorLog ${APACHE_LOG_DIR}/djangoprojects-error.log
   CustomLog ${APACHE_LOG_DIR}/djangoprojects-access.log combined

-  Remember that we opened the door for apache writing with the chmod 777 to the demo subdir before, THIS HAS TO BE CHANGED using a proper location for www-data user access...

Everything is now in place, activate the new virtual apache environment and reload apache configuration.

sudo a2ensite rdkitDemo
sudo service apache2 reload

You should now have a great django web site to play with, just integrate your rdkit applications!

To ensure everything is up and running, open your brower to the location


In case you will be in trouble, check apache logs in /var/log/apache2/djangoprojects-error.log, they are isolated to better identifiy mistakes and / or copy/pasting errors.


Comment on this article