Implementation of desktop notifications using Python

Introduction

--Here is a summary of desktop notifications using python. --It can be easily implemented by using the python library. ――The following is a usage example (it is just an example that it may be used in this way, and I think it can be used for more general purposes) --Notify when the battery level of a certain product is less than xx% of the battery level, assuming that there is a database that stores it in real time. --Notify when the memory usage of a server exceeds 80%, assuming that there is a database that stores the memory usage in real time. ――I think that using it well will help improve work efficiency.

Execution environment

Implementation example

--By executing the following source code, you can achieve the following. --Every 10 seconds, SQL is issued to the DB table to get the value, and if the threshold is exceeded, the desktop is notified. ――Since this is an example, it is a simple table that contains the following time and data, but I think that it can be expanded in various forms such as using View etc. to make the data averaged for 1 hour. image.png


from plyer import notification
import psycopg2
import schedule
import time

def job(cur):
    alert = ""

    cur.execute("select data from sample order by time desc limit 1;")
    rows = cur.fetchall()
    hoge = rows[0][0]

    if int(hoge) >= 98:
        alert += "hoge is 100%Is approaching."

    if alert != "":
        notification.notify(
            title = "warning",
            message = alert,
            app_name = "Monitor monitoring"
        )


print("Start monitoring.\n A desktop notification will be sent when the reference value is reached.")

#Connection information creation
con = psycopg2.connect("host=xxx.xxx.xxx.xxx port=xxxx dbname=xxxx user=xxxx password=xxxx")
cur = con.cursor()

#Execute job every specified time (every 10 seconds this time)
schedule.every(10).seconds.do(job, cur)

while True:
    try:
        schedule.run_pending()
        time.sleep(1)
    except KeyboardInterrupt:
        print("It was interrupted.")
        cur.close()
        con.close()
        break

Actual notification screen

--The following desktop notifications are sent when the threshold is exceeded. image.png

Recommended Posts

Implementation of desktop notifications using Python
Implementation of TF-IDF using gensim
python: Basics of using scikit-learn ①
Python implementation of particle filters
Implementation of quicksort in Python
[Python] Implementation of clustering using a mixed Gaussian model
Image capture of firefox using python
Python implementation of self-organizing particle filters
Removal of haze using Python detailEnhanceFilter
Implementation of life game in Python
View Mac desktop notifications from Python
Python implementation of non-recursive Segment Tree
Implementation of Light CNN (Python Keras)
Implementation of original sorting in Python
Implementation of Dijkstra's algorithm with python
Python: Basics of image recognition using CNN
[Python] Extension using inheritance of matplotlib (NavigationToolbar2TK)
Automatic collection of stock prices using python
About building GUI using TKinter of Python
(Bad) practice of using this in Python
Implementation of dialogue system using Chainer [seq2seq]
Python: Application of image recognition using CNN
Recommendation tutorial using association analysis (python implementation)
Implementation of "blurred" neural network using Chainer
Study on Tokyo Rent Using Python (3-1 of 3)
Non-recursive implementation of extended Euclidean algorithm (Python)
Python implementation of continuous hidden Markov model
Meaning of using DI framework in Python
Introduction of Python
Basics of Python ①
Basics of python ①
Copy of python
Scraping using Python
Introduction of Python
Time variation analysis of black holes using python
Chord recognition using chromagram of python library librosa
Why the Python implementation of ISUCON 5 used Bottle
Introduction of Python Imaging Library (PIL) using HomeBrew
Character encoding when using csv module of python 2.7.3
Implementation of TRIE tree with Python and LOUDS
Try using the collections module (ChainMap) of python3
Anonymous upload of images using Imgur API (using Python)
[Codeing interview] Implementation of enigma cryptographic machine (Python)
Find the geometric mean of n! Using Python
Python introductory study-output of sales data using tuples-
Explanation of edit distance and implementation in Python
Summary of Excel operations using OpenPyXL in Python
[Python] Operation of enumerate
Implementation example of simple LISP processing system (Python version)
Maximum likelihood estimation implementation of topic model in python
Operate Redmine using Python Redmine
Explanation of the concept of regression analysis using python Part 2
RNN implementation in python
Basics of I / O screen using tkinter in python3
[Python] [Word] [python-docx] Simple analysis of diff data using python
Fibonacci sequence using Python
Unification of Python environment
Copy of python preferences
Cut a part of the string using a Python slice
Data analysis using Python 0
Basics of Python scraping basics