DEV Community

Everton Tenorio
Everton Tenorio

Posted on

Displaying Python Script Outputs on Conky Panels

In this post, I'll demonstrate a simple way to display data from API requests directly on desktop panels using Python and Conky.

Objective

The goal is to fetch information from an API and show it on a desktop panel. For this example, I'll use Python for the API requests and Conky to create the panels.

We’ll fetch the Bitcoin exchange rates in USD and BRL using the economia.awesomeapi.com.br API. Then, we’ll configure Conky to execute the Python script every hour and display the output on the panel. I’ve also added some basic styling to make the panel look better.

btc panel


Python Script: btc_data.py

Below is the Python script that retrieves Bitcoin rates and formats the output for the Conky panel:

import requests

API_URL = "https://economia.awesomeapi.com.br/json/last/BTC-USD,BTC-BRL"

try:
    response = requests.get(API_URL)
    data = response.json()

    btc_usd = data.get("BTCUSD", {})
    btc_brl = data.get("BTCBRL", {})

    usd_alta = f"$${float(btc_usd.get('high', 'N/A')):,.2f}"
    usd_baixa = f"$${float(btc_usd.get('low', 'N/A')):,.2f}"

    brl_alta = f"R$${float(btc_brl.get('high', 'N/A')):,.2f}"
    brl_baixa = f"R$${float(btc_brl.get('low', 'N/A')):,.2f}"

    formatted_data = (
        "\n\n${color white}BTC - USD\n${color}${color green} High: ${color}${color white}"+usd_alta+"\n${color red} Low: ${color}${color white}"+usd_baixa+"\n\n"
        "${color white}BTC - BRL\n${color}${color green} High: ${color}${color white}"+brl_alta+"\n${color red} Low: ${color}${color white}"+brl_baixa+"\n"
    )

    print(formatted_data)

except Exception as e:
    print(e)

Enter fullscreen mode Exit fullscreen mode

Conky Configuration: btc_ck.conf

Here’s the configuration file for Conky. It runs the Python script every hour (3600 seconds) and displays the formatted output:

conky.config = {
    default_color = '#afafaf',
    own_window = true,
    own_window_type = 'normal',
    own_window_transparent = true,
    own_window_colour = '#000000',
    own_window_hints = 'undecorated, skip_taskbar',
    use_spacer = 'right',
    border_inner_margin = 20,
    alignment = 'middle_right',
    use_xft = true,
    double_buffer = true,
    font = 'Monospace:size=8:style=semibold',
    gap_x = 80,
    update_interval = 1.0,
}

conky.text = [[
${image /home/.../bitcoin-btc-logo.png -n -p 50,1 -s 25x25}
${execpi 3600 python3 /home/.../btc_data.py}
]]
Enter fullscreen mode Exit fullscreen mode

Key Points:

  • API Data: Fetching Bitcoin’s high and low prices in both USD and BRL.
  • Update Frequency: The panel updates every hour via the execpi function.
  • Styling: Some basic customization is applied to improve the panel’s appearance.

Running the Project

  • Save the Python script (btc_data.py) and the Conky configuration file (btc_ck.conf) in the desired directory.
  • Update the file paths in btc_ck.conf as needed (e.g., Python script location, Bitcoin logo image).
  • Start Conky with the configuration:
conky -c /path/to/btc_ck.conf
Enter fullscreen mode Exit fullscreen mode

Top comments (0)