In this post, I'll demonstrate a simple way to display data from API requests directly on desktop panels using Python and Conky.
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.
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)
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} ]]
conky -c /path/to/btc_ck.conf
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!