13

I have a python3.4 script. I would like to send to the desktop a notification. How do I handle this in python? Can I use notify-send?

I'm using Ubuntu 14.04.

#in my script
if something:
  notify-send 'Here is a notification !'
muru
  • 207,970
TotuDoum
  • 163

2 Answers2

27

You can use notify-send as an external command:

import subprocess as s
s.call(['notify-send','foo','bar'])

Or you can use the notify2 module (sudo apt install python3-notify2):

import notify2
notify2.init('foo')
n = notify2.Notification('foo', 'bar')
n.show()

There are more examples included in the package (see /usr/share/doc/python3-notify2/examples/).

muru
  • 207,970
  • @sgiri I thought the external lib would be better than subprocess. In either case you have to install something anyway. – Hey Jul 11 '17 at 13:33
  • @YdobEmos , subprocess is the standard library that is distributed with Python. Hence, no need to install it as a third party library. [ref: https://docs.python.org/2/library/index.html] – sgiri Jul 13 '17 at 15:41
  • You have to install the packet supplying notify-send. Or at least on Kubuntu you have to. Maybe it's provided by default on Ubuntu, in that case it's indeed the best solution. – Hey Jul 13 '17 at 20:34
4

No specific dependencies needed. These are wrappers around dbus anyway.

import dbus

bus_name = "org.freedesktop.Notifications" object_path = "/org/freedesktop/Notifications" interface = bus_name

notify = dbus.Interface(dbus.SessionBus().get_object(bus_name, object_path), interface) notify.Notify(ARGS)

For the args see the specs.