20 Oct 2011

python-notify example

Tags: python, python-notify, example, examples, tutorial, pynotify, py-notify

So, I was playing around with python-notify just today, and I was amazed by how hard it is to find any examples not to mention atleast some documentation on it.

python-notify is developed and maintained by the guys over at galago-project.org who also developed libnotify. So python-notify is really just some python bindings for libnotify.

It's incredible how easy it is to use, but you still do need some examples, because well, you can't really guess what to write when you have no idea what anything in the library is called, nor how it works.

So I have decided to write some small examples using the library, also for future references for myself :)

Example 1

This example just shows a basic notification, nothing fancy here really.

example1.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/usr/bin/env python
# encoding: utf-8

import pynotify

''' libnotify needs some init value,
it really can be anything, it just uses it
to differentiate between the popups
'''
pynotify.init("Basic")

n = pynotify.Notification("Title",
  "Some sample content"
)

n.show()

Output:
example 1

Example 2

This example shows the use of some simple HTML markup that libnotify supports.

example2.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/usr/bin/env python
# encoding: utf-8

import pynotify
pynotify.init("markup")

n = pynotify.Notification("Markup",
  '''
  <b>bold</b>, <i>italic</i>, <u>underline</u>
  and even <a href="http://google.com">links</a> are supported!
  '''
)

n.show()

Output:
example 2

Example 3

This example shows the use of an image inside a notification. It will need an absolute path since the notification is handled by the notification daemon, and not by python itself.

example3.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/usr/bin/env python
# encoding: utf-8

import pynotify
pynotify.init("image")

n = pynotify.Notification("Image",
  "Hurray! an Image!",
  "/absolute/path/to/image.png",
)

n.show()

Output:
example 3

That's it! It really is that simple.

Now go make something useful of it!