Sunday, August 5, 2012

Django 1.4 for Android (only dev. runserver!)

 

I’m writing this post because of this interesting article:
(I’m sorry that it’s written in Korean)

[기고]약정 벗은 안드로이드, 서버가 되다…'서품폰'
http://www.zdnet.co.kr/column/column_view.asp?artice_id=20120518070549

This article, which explains how to make a old android phone into simple server with web2py and ubuntu blahblah, concluded that the post’s author couldn’t set up Django on Android. I can’t understand why – you know, Django doesn’t depend on a bunch of ‘native’ python packages as I know(in other words, Django consists of pure python source files). If python is configured correctly, Django goes well as well, I guess.

Yes, my Django doesn’t go such a fastidious way.

 

케냘님의 미투포토

 

… So spent half a day, finally I got working Django 1.4 on Android – the working feature is mere runserver thing yet. Whatsoever, with more efforts, I believe that it’s going to be able to set up Django with other web server program. Will mod_wsgi be excutable on Android? hmm…

However, here’s my work:

※ Note: Target device is Motorola MOTOGLAM, rooted and Android Froyo (2.2) installed.

※ Note: The Android is unlike a typical Linux environment, the packages that can be installed in are limited. Django on Android  has such limitation as well. It’s hard to install the packages for Django(e.g. south, celery …), even sometimes impossible it is. So, just for fun, okay?

 

 


Django 1.4 for Android
(only dev. runserver!)

 

First, you have to install few apps.

Some can be installed through Google Play, and others sholud be installed manually by using apk package. Heck, you don’t know about apk? DON’T DO THIS. Headache might overwhelm you.

The following apps being on Google Play:

 

- File Manager

This is for installing apk or in case deleting some file manually. I’m using ES File Explorer as file manager.

 

- Terminal Emulator

Virtual terminal for Android. If you don’t wanna root your phone, I guess there are some ways to do this, can you type all of tedious strings with Android’s virtual keyboard? Forget it. Use SSH, for your precious soul.


 

- SSHDroid

A sort of ssh deamon for Android. It makes basic unix workspace for you with some utilities which don’t exist on Android.

Launching SSHDroid, you can check the device’s IP address. And password for connection is configurable on SSHDroid’s menu. (You might change this)

 

 

 

The following apps are what you should install using apk manually:

 

 

- Rooting tool

This work might be done without rooting, but I have no idea on this. I guess it’s real hell without rooting, just do this. You can find vary tools for your Android – and it depends on what device is and what Android OS version is. So I omitted rooting things here.

If your device is rooted, run SSHDroid on your device,

confirm that you could connect to your device through ssh. If you’re mac user, you could use default terminal program. In case of windows user, you could use ssh client program such a putty. According to above image, 10.0.0.14 is IP address of Android device. If ssh connected, you see messages like these:

Kenials-MBA:~ kenial$ ssh root@10.0.0.14

The authenticity of host '10.0.0.14 (10.0.0.14)' can't be established.
RSA key fingerprint is f7:97:44:c3:ab:49:42:14:db:ec:f2:e2:b8:ae:62:74.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.14' (RSA) to the list of known hosts.
SSHDroid
Use 'root' on rooted devices otherwise any username works
Default password is 'admin'
root@10.0.0.14's password:

/data/data/berserker.android.apps.sshdroid/home #

 

 

- SL4A (Scripting Layer for Android) http://code.google.com/p/android-scripting/
- P4A (Python for Android)
http://code.google.com/p/python-for-android/

You guess what, from these names? These guys make your Android device to execute Python script on it, easily. Django is a web framework written in python, so python environment to run is needed.

Connecting to your device through ssh, install them(I downloaded thme at /mnt/sdcard/_apk)

cd /mnt/sdcard/_apk
wget http://android-scripting.googlecode.com/files/sl4a_r6.apk
wget http://python-for-android.googlecode.com/files/PythonForAndroid_r6.apk
su
pm install sl4a_r6.apk
pm install PythonForAndroid_r6.apk

 

Then, check your apps installed, SL4A and Python for Android.

 

Runs Python for Android app, then tap Install button to install modules:

 

 

If the installation done, run SL4A  app and execute helloworld.py script to check python works correctly.

 

- Edit .profile

This .profile script is for configuration of python environment. In terminal(or ssh client), type following commands to edit .profile:

cd
vi .profile

 

The content of .profile file:

PATH=/data/data/berserker.android.apps.sshdroid/home/bin:$PATH
export PATH
export EXTERNAL_STORAGE=/mnt/sdcard                                                                                                                                                                                                 
export LANG=en
PYTHONPATH=/mnt/sdcard/com.googlecode.pythonforandroid/extras/python
PYTHONPATH=$PYTHONPATH:/data/data/com.googlecode.pythonforandroid/files/python/lib
PYTHONPATH=$PYTHONPATH:/data/data/com.googlecode.pythonforandroid/files/python/lib/python2.6/lib-dynload
export PYTHONPATH
export TEMP=/mnt/sdcard/com.googlecode.pythonforandroid/extras/python/tmp
export PYTHON_EGG_CACHE=$TEMP
export PYTHONHOME=/data/data/com.googlecode.pythonforandroid/files/python
export LD_LIBRARY_PATH=/data/data/com.googlecode.pythonforandroid/files/python/lib

# create /usr path for django-admin.py
if [ ! -d /usr/bin ];
then
     mount -o remount,rw /
     mkdir /usr
     ln /data/data/berserker.android.apps.sshdroid/home/bin /usr/bin –s
fi
sh 

 

Save the file and quit. Then type (or just reconnect to device):

sh .profile

 

Link python to sshdroid’s default path:

ln /data/data/com.googlecode.pythonforandroid/files/python/bin/python /data/data/berserker.android.apps.sshdroid/home/bin/python
ln /data/data/com.googlecode.pythonforandroid/files/python/bin/python /data/data/berserker.android.apps.sshdroid/home/bin/python2.6

 

Try to run python interpreter. If works, you see the result:

 

 

- Django installation

Now (finally!!), it’s time to install Django.

Go /mnt/sdcard/_apk path, download Django 1.4 package file, unzip it, copy the ‘django’ folder to python’s library folder. (Time to unzip could take a long time, just copy unzipped folder from PC directly into sdcard, if you want)

cd /mnt/sdcard/_apk
wget http://www.djangoproject.com/m/releases/1.4/Django-1.4.tar.gz
tar xzvf Django-1.4.tar.gz
cd Django-1.4
cp -r django /data/data/com.googlecode.pythonforandroid/files/python/lib/python2.6/lib-dynload/django     # instead of 'python setup.py install'
ln /data/data/com.googlecode.pythonforandroid/files/python/lib/python2.6/lib-dynload/django/bin/django-admin.py /data/data/berserker.android.apps.sshdroid/home/bin/django-admin.py

Here is one what you remember: In most cases, you can’t install python package using general setup.py script on Android. Android OS doesn’t have various developer’s tools, for example, like gcc, so you need to copy django folder into lib-dynload folder by hand. Then python could load Django modules from lib-dynload folder, using by p4a.

* You can also let this Django folder go to site-packages folder, which is more general for pythonic package. But site-packages of P4A is on sdcard path, might cause slow performance(it depends on what your device is). So I copied it to lib-dynload folder under /data path.

ln command will link django-admin.py to SSHDroid’s PATH enviroment variable, and you can django-admin.py on anywhere path.

 

- Create django project

Wanna take a bite? Go ahead:

cd /mnt/sdcard
django-admin.py startproject django_first
cd django_first
python manage.py runserver 0.0.0.0:8000

 

Check it out running:

 

And bring it on:

 

Done! : )

2 comments:

  1. Using Kivy, Django can also run on Android as a background service: https://github.com/blagarde/djandro

    ReplyDelete
    Replies
    1. Interesting. I had known about Kivy, but I didn't notice it could work for this. I should try on.

      Thanks tistou!

      Delete