Home > Backend Development > Python Tutorial > Displaying Python Script Outputs on Conky Panels

Displaying Python Script Outputs on Conky Panels

Linda Hamilton
Release: 2024-12-28 17:37:10
Original
909 people have browsed it

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.

Displaying Python Script Outputs on Conky Panels


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)

Copy after login

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}
]]
Copy after login

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
Copy after login

The above is the detailed content of Displaying Python Script Outputs on Conky Panels. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template