Desktop Notifications from Python Scripts

Desktop Notifications from Python scripts

  • Vuyisile Ndlovu
  • @terrameijar on Twitter & Github

Agenda

  • In this talk, I will give some background on common dialogs we are used to and how you can add these to your commandline apps
    • Why users like Graphical User Interfaces?
    • How to add GUI to scripts
    • Sending notifications to users
    • Questions
  • Questions and Answers

The terminal…

Commandline applications are great because they are fast, easy on resources and allow you to automate repetitive tasks in the terminal. In this talk, I will show you two ways to bring your commandline applications out of the terminal and onto your desktop.

Building applications using GUI toolkits

Tkinter is …

the standard Python interface to the Tk GUI toolkit and is Python's de facto standard GUI.

Why make Graphical User Interfaces?

  • GUI toolkits like Tkinter make your application more user friendly
  • GUI applications are more visually appealing
  • There are many GUI toolkits to choose from e.g PyGtk, PyQt and wxPython.

GUI Features we like.

All GUI toolkits allow us to use standard windows such as these:

Common GUI dialogs

File/Directory Selection

Common GUI dialogs

Common GUI dialogs

Question Boxes

GUI CONS

  • GUI cannot be used all the time.
  • GUIs require more resources
  • GUIs require one to write lots of code just for the interface.

Wouldn't it be great if we could "borrow" some of the GUI features and add them to our scripts without turning them into full-featured GUI programs?

Zenity is …

a utility used to add GUI forms to scripts and receive feedback from the user.

Zenity

  • Zenity enables you to create various simple dialogs that interact graphically with the user.
  • Useful for creating quick scripts that get the job done
  • Is installed by default on Ubuntu, and is available on most Linux distributions
  • Can be called from Python using the subprocess module
  • Alternatively Zenity can be invoked using the PyZenity module.

POSIX shell script Example

        
        #!/bin/sh

         zenity --question \
          --text="Are you sure you wish to proceed?"
        
      

Python Example

         
    #! /usr/bin/env python
    import subprocess as s

    s.call(["zenity","--question","--text=Are you sure?"])
         
       

PyZenity Example


  #! /usr/bin/env python
  from PyZenity import Question

  cancel = Question('Should I cancel the operation?')

      

Demo

Desktop Notifications

notify-send is a program used to send notifications to the user via a notification daemon from the commandline. These notifications can be used to inform the user about an event or display information without getting in the user's way.

notify-send:

  • shows pop up notifications
  • notifications disappear after a few seconds
  • Installed by default in Ubuntu, available for other distros
  • notifications are not modal
  • lightweight

USAGE EXAMPLE

notify-send [OPTION] [TITLE] [BODY]
  • For example, notify-send --icon weather-overcast \ "Weather" "Overcast"

Python Usage Example

      
import subprocess as s

s.call(["notify-send", "--icon", "weather-overcast", \
    "Weather", "Overcast"])
      
    
  • Use the subprocess module to call notify-send
  • Options are passed as a list

Resources

Questions?

Thanks

that's all firefox