GitHub30 / win11toast

Toast notifications for Windows 10 and 11 based on WinRT
https://pypi.org/project/win11toast/
MIT License
227 stars 15 forks source link

icon and image work only with absolute path #32

Open zFishStick opened 7 months ago

zFishStick commented 7 months ago

I don't know why but when i create a toast with an absolute path icon it works, but when i use a relative or a URL it doesn't work. I tried with the example given by the dev: toast('Hello', 'Hello from Python', icon='https://unsplash.it/64?image=669'), but nothing, it prints the text but nor the image.

jakub-antos commented 5 months ago

Use Base64 to decode image from code to file (temp), and next use it.

from win11toast import toast import base64 import os

Your Base64 code for the icon

icon_base64 = "your_base64_code_here"

Decode Base64 code into bytes

icon_data = base64.b64decode(icon_base64)

Path to the temporary folder

temp_folder = os.environ['TEMP']

Path to save the icon file

icon_path = os.path.join(temp_folder, 'icon.png')

Save the icon file

with open(icon_path, 'wb') as icon_file: icon_file.write(icon_data)

Create a link to the icon as an absolute path

icon_url = 'file://' + icon_path

Define the icon data

icon = { 'src': icon_url, 'placement': 'appLogoOverride' }

Display the notification with the icon

try: toast('Hello', 'Hello from Python', icon=icon) except Exception as e: print("An error occurred while displaying the notification:", e)