DEV Community

Cover image for Making modules for Polybar (Shell + Python)
Haider Ali Punjabi
Haider Ali Punjabi

Posted on • Originally published at

Making modules for Polybar (Shell + Python)


I started using Manjaro i3 back in December 2018, after 3 years of Ubuntu and one unsuccessful attempt at installing Arch. Being a long time lurker of r/unixporn, I knew what I had to do. After learning the ropes of Manjaro, the first thing I worked on was ricing my installation.

I initially installed Polybar with some config I found on r/unixporn. A few weeks later, u/adi1090x posted on Reddit his beautiful polybar themes. I immediately fell in love (being a huge material fan) and started using them. 5 months later, I still use v2 of his themes.

In his work, he had used rofi to act as a drop down for polybar. Many think that I came up with it, but I first saw it in u/adi1090x 's work.

Since I was using polybar, indicator-kdeconnect wasn't working for me. It was an important part of my daily usage, and I missed it a lot. After coming up with no way of using it with polybar, I decided to look into making one of my own, thereby starting my journey into polybar modules.

polybar-kdeconnect v1

I started by learning how polybar modules work. Any script (python, bash, etc) that can be executed in a shell can be used as a module. The output of the script is what is shown in polybar. Polybar also allows executing scripts on events such as click, right-click etc.

The v1 of my kdeconnect module was very simple. A script that used kdeconnect-cli (part of kdeconnect) to check whether a device was connected or not, and show an icon based on that. It didn't support multiple devices, and the only way was to create copies of the module.

After sharing that on Reddit, I received lots of feedback, suggestions and ideas to improve the module.

A day later, I had implemented different colours for different battery levels.

After learning about polybar modules a bit more (I will link to the resources at the end), I was ready to give the kdeconnect module another shot. The v2 was entirely different to v1, so much so that I had to rewrite the whole (60 lines approx.) module.

polybar-kdeconnect v2

Two weeks after v1, I release the polybar-kdeconnect v2. Among the many improvements, the following were the most important

  • no dependency on kdeconnect-cli
  • Multiple Device support with extra config
  • Parinig / Unpairing from the module
  • Single script

The best improvement was adding multiple device support. It worked by going over every device kdeconnect knows, which were of 3 types

  • Connected (Reachable & Trusted)
  • Not Connected (Not Reachable & Trusted)
  • Available (all that were left)

Based on their type, the script would append an icon with a separator to an output string. After all devices were checked, the output string would be printed (echoed??) and displayed on polybar.

To make every icon clickable separately, I used action format tags (the %{A} things in the code).

Action format tags work by executing some command. Since I couldn't call a function by its name directly, this script wasn't loaded in the environment the action tag used the command, I had to source my script and then call the function and pass the parameters.

Here's the appending to the output part

devices+="%{A1:. $DIR/; show_pmenu $devicename $deviceid $:}$icon%{A}$SEPERATOR"

Note that this isn't what my final code looks like

Since I am not much experienced with shell scripting, many contributors did some overhauling of my code (adding removing quotes etc).

Some issues were also opened, some were resolved some are still open.

To fix problems with the code, and follow some conventions, I added getopts to the module. I always wondered about the ability of commands to use parameters (-f, -x etc). If you, like me don't know how to implement them, getopts is the answer.

To prevent bugs, I wrote my other module in Python. I am better at Python than at Shell. I thought about rewriting this in Python, but its heavy reliance on 'rofi' dissuaded me.

Check out the latest polybar-kdeconnect on Github


I usually use mpd for my music, and the mpd module for polybar works great. While thinking about using Python to write polybar modules, I thought about making a media control for browsers. It would be very similar to the mpd module.

The first issue was to send commands to any browser. AFAIK, one needs to host some sort of server on the machine which sends a command to an installable browser extension. Since I had no experience of it, nor the desire to write too much code for a simple task, I decided to use Plasma Browser Integration which is a great addition to kdeconnect.

Plasma Browser Integration enables a user to send links from a browser to an android device using KDEConnect and also allows to control browser media from an android device. Technically, it exposes some DBus endpoints which KDEConnect can use to control media of the browser like it controls other media players.

I also use the same endpoints in my module.

The module is very simple compared to the kdeconnect one. I use pydbus to connect to the DBus endpoints, and argparse (python equivalent of earlier mentions getopts) to add a couple of arguments to the scripts.

Earlier mentioned action format tags were again used for making the play/pause button.

Check out the latest polybar-browsermediacontrol on Github

Important Resources

Top comments (1)

nafg profile image

"action format tags" links back to this page