Skip to content

Instantly share code, notes, and snippets.

@clungzta
Created November 30, 2020 02:51
Show Gist options
  • Save clungzta/89d9e4912721ae0adc4c9c8e9610a27b to your computer and use it in GitHub Desktop.
Save clungzta/89d9e4912721ae0adc4c9c8e9610a27b to your computer and use it in GitHub Desktop.
Ubuntu indicator for NVIDIA GPU's. Displays utilisation, memory usage, and temperature
import gi
import nvsmi
import signal
import time
from threading import Thread
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk, AppIndicator3, GObject
class Indicator():
def __init__(self):
self.app = 'gpu-indicator'
iconpath = "/home/alex/Downloads/video-card.png"
self.indicator = AppIndicator3.Indicator.new(
self.app, iconpath,
AppIndicator3.IndicatorCategory.OTHER)
self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
self.indicator.set_menu(self.create_menu())
self.indicator.set_label(self.generate_label(), self.app)
# the thread:
self.update = Thread(target=self.indicator_loop)
# daemonize the thread to make the indicator stopable
self.update.setDaemon(True)
self.update.start()
def generate_label(self):
return ', '.join([f"GPU {gpu.id}: {gpu.gpu_util: <8}%, {int(gpu.mem_used)}MB/{int(gpu.mem_total)}MB, {gpu.temperature}\u2103" for gpu in nvsmi.get_gpus()])
def indicator_loop(self):
t = 2
while True:
time.sleep(1)
# apply the interface update using GObject.idle_add()
GObject.idle_add(
self.indicator.set_label,
self.generate_label(), self.app,
priority=GObject.PRIORITY_DEFAULT
)
t += 1
def create_menu(self):
menu = Gtk.Menu()
# menu item 1
item_1 = Gtk.MenuItem('Menu item')
# item_about.connect('activate', self.about)
menu.append(item_1)
# separator
menu_sep = Gtk.SeparatorMenuItem()
menu.append(menu_sep)
# quit
item_quit = Gtk.MenuItem('Quit')
item_quit.connect('activate', self.stop)
menu.append(item_quit)
menu.show_all()
return menu
def stop(self, source):
Gtk.main_quit()
Indicator()
signal.signal(signal.SIGINT, signal.SIG_DFL)
Gtk.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment