Django Tips & Tricks #8 - Hyperlink Foreignkey Fields In Admin


Consider Book model which has Author as foreignkey.

from django.db import models


class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.ForeignKey(Author)

We can register these models with admin interface as follows.

from django.contrib import admin

from .models import Author, Book

class BookAdmin(admin.ModelAdmin):
    list_display = ('name', 'author', )

admin.site.register(Author)
admin.site.register(Book, BookAdmin)

Once they are registed, admin page shows Book model like this.

While browing books, to go to a particular author, we have to previous page, go to Author model and then find relevant author. This becomes tedious if we spend lot of time in admin. Instead, if author field has a hyperlink, we can directly go to its page.

Django provides an option to access admin views by its URL reversing system. For example, we can get add view of author model in book app from reverse("admin:book_author_add").

To hyperlink author field in book admin, get url from reversing book_author_change with its id and return required html.

class BookAdmin(admin.ModelAdmin):
    list_display = ('name', 'author_link', )

    def author_link(self, book):
        link = reverse("admin:book_author_change", args=[book.author.id])
        return u'<a href="%s">%s</a>' % (link, book.author.name)
    author_link.allow_tags = True
    author_link.short_description = 'Author'

Now in the book admin view, author field will be hyperlinked and we can visit just by clicking it.

Update:

Django has inbuilt option for this. It provides list_display_links, to control which fields should be linked to change page. So, we can just add author field to it.

from django.contrib import admin

from .models import Author, Book

class BookAdmin(admin.ModelAdmin):
    list_display = ('name', 'author', )
    list_display_links = ('name', 'author',)

admin.site.register(Author)

Now, author field will be hyperlinked to its change page.

Comments

How To Remove Clock From LockScreen/StatusBar?


Last year, I wrote a blog post on how to remove clock from lock screen and status bar if xposed is installed on your android device. You can also do this without xposed if you are using RR(Resurrection Remix) as it comes with a lot of inbuilt customization.

Remove Clock From LockScreen

To remove clock from lockscreen, go to Settings -> Configurations -> Lock screen -> Show lock screen lock.

Remove Time From StatusBar

To remove clock from status bar, go to Settings -> Configurations -> Status bar -> System UI tuner -> Time -> Dont show this icon.

Once you do this, you will have a clean lockscreen and statusbar without any date or time them.

Comments

Bluetooth Serial Communication Between Ubuntu & Android


Most laptops and smart phones(Android/iPhone) have builtin Bluetooth modules. We can use this bluetooth module to communicate with each other or with other bluetooth modules like HC-05 or HM-10.

In this article, we will learn how to send data between laptop and android bluetooth.

First, we need to pair with a bluetooth device to send information. From Ubuntu, we can pair to a Bluetooth device from Bluetooth settings. Alternatively, we can also use CLI to do the same.

$ bluetoothctl
[NEW] Controller 24:0A:64:D7:99:AC asus [default]
[NEW] Device 94:E9:79:BB:F8:3A DESKTOP-C4ECO3K
[NEW] Device 88:79:7E:7B:4C:87 athene
[NEW] Device 94:65:2D:8C:2E:10 OnePlus 5
[NEW] Device 98:0C:A5:61:D5:64 Lenovo VIBE K5 Plus
[NEW] Device AC:C3:3A:A0:CE:EF Galaxy J2
[NEW] Device 98:D3:35:71:02:B3 HC-05

[bluetooth]# power on
Changing power on succeeded

[bluetooth]# agent on
Agent registered

[bluetooth]# default-agent
Default agent request successful

[bluetooth]# scan on
Discovery started
[CHG] Controller 24:0A:64:D7:99:AC Discovering: yes
[CHG] Device 94:E9:79:BB:F8:3A RSSI: -88
[CHG] Device 88:79:7E:7B:4C:87 RSSI: -66

[bluetooth]# pair 88:79:7E:7B:4C:87
Attempting to pair with 88:79:7E:7B:4C:87
[CHG] Device 88:79:7E:7B:4C:87 Paired: yes
Pairing successful

To communicate with paired devices, we will use RFCOMM protocol. RFCOMM is just a serial port emulation and provides reliable data tranfer like TCP.

From ubuntu, lets open a port for communication.

$ sudo rfcomm listen /dev/rfcomm0 3

From Android, we have to connect to ubuntu. For this, we can use Roboremo app which supports RFCOMM.

$ sudo rfcomm listen /dev/rfcomm0 3
Waiting for connection on channel 3
Connection from 88:79:7E:7B:4C:87 to /dev/rfcomm0
Press CTRL-C for hangup

Once the connection is established, we can communicate between devices.

In Unix like systems, OS provides a device file as an interface for device driver. To send and read messages from Linux or Mac is as easy as reading and writing to a file.

# to send message to bluetooth
$ echo 'hello from ubuntu' > /dev/rfcomm0

We can see the received messages on Android

We can also send messages from android and read from ubuntu.

# to read messages from bluetooth
$ cat /dev/rfcomm0
hello from android

This way, we can communicate with any bluetooth module using a laptop or a smart phone.

Comments

Amazon India Artificial Intelligence (AIAI) Summit


Today I have attended AIAI summit 2017 in Bangalore. Amazon started this summit to network with academia, industry data scientists and showcase amazon's work in machine learning.

There were 4 talks and 20 poster presentations. Most of them were scholars from IITB, IITM, IISC, IITH, one talk by CMU professor and remaining talks by Amazon.

Amazon team talked about the problems they are solving with machine learning like auto correcting address, suggesting right size for shoes/apparel, answering questions on products e.t.c.

People from academia talked about the research work they are doing and their results.

I have been working on Telugu OCR in the past few months. There were 3 poster presentations regarding indic languages. Even though a lot of papers were published in the past, there is no end-to-end OCR with good accuracy (>98%) for Indic languages. It is good to see Indic languages getting traction in deep learning.

Comments

Arduino Programming From Text Editor & CLI


To program Arduino, we can use Arduino IDE which facilitates writing and uploading code to the board.

If we are using a text editor for programming, it will have lot of customisations which speed up development process. In such case, it is better to use same text editor for arduino programming too.

I use Emacs as IDE and there is arduino mode for emacs which provides syntax highlighting and some useful utilites to write arduino code. We can find such packages for other editors also.

Arduino also provides cli interface to upload code to arduino. To upload code, we need to specify port, board and the code to upload.

In Linux system, to upload a file called foo.ino, we can run

arduino --port /dev/ttyACM0 --board arduino:avr:mega
 \ --upload foo.ino

An alternate way is to use platformio, an opensource tool chain for IoT development.

It can be installed using pip.

pip install platformio

Once it is installed, code can be directly uploaded using ci command.

pio ci --board=megaatmega2560 --project-option="upload_port=/dev/ttyACM0" --project-option="targets=upload" foo.ino

By this we can use text editor to write code and arduino/platformio to upload code to arduino board.

Comments

Using LSTM-CTC For Complex Script Recognistion


Most Indian languages have strong consonant-vowel structure which combine to give syllables. These syllables are written as one continuous ligature and they require complex text rendering (CTL) for type setting.

Writing OCR (Optical Character Recognistion) software for CTL scripts is a challenging task as segmentation is hard. Because of this overall accuracy drops drastically.

A better approach is to use Connectionist Temporal Classification (CTC) which can identify unsegmented sequence directly as it has one-to-one correspondence between input samples and output labels.

Here is a sample input and output of a RNN-CTC network which takes an unsegmented sequence and outputs labels.

Open source OCR software ocorpy uses BLSTM-CTC for text recognistion. Tesseract started using the same in its latest(4.0) version.

I have trained a model to recognize Telugu script using ocropy and the accuracy is ~99% which is far better when compared to OCR softwares without CTC which are accurate to ~70%.

Comments