Package: ntfy-wrapper
ntfy-wrapperis a free and hassle-free customizable notifier for Python. No login, no API token, no fees, no bullshit.It's actually a simple Python wrapper aroundntfy. Kudos to them ❤️You can nowsendnotification from your Python code andreceivethem on your computer through aWeb Appor aCLI, oron your phone with a dedicated app!Again, all credit tontfy.InstallInstallntfy-wrapperwithpip:pipinstallntfy-wrapperDependencies:requestsfor easy HTTP requests and interacting with thentfy.shAPItyperfor a powerful and beautiful CLIxkcdpassto generate secure but human-friendly topics[^1]Why?Imagine you execute jobs on a shared cluster. They are in a queue. You get notified when they start. You get notified if there's an error. You get notified with the final performance of your model and if you click on the notification, you have access to the online logs from wandb or comet.ml.That's just one scenario inspired from my work in AI, but really you can use it in any context!Getting Startedfromntfy_wrapperimportNotifierif__name__=="__main__":ntfy=Notifier(notify_defaults={"title":"Your Project Name"})# IFF this is the first call to `ntfy_wrapper` in your project, this line ^# will print a topic id.# It will also write the topic id to `.ntfy.conf` so this only happens once!## Use one of those methods (from the print or the conf file) to copy the topic id# and use it to subscribe to notifications from the web or mobile apps.## You can also use the command-line `$ py-ntfy init` before executing your code# and the same process will happen (new topic + conf file)## Note: anyone with your topic id can subscribe to your notification. It's probably# best to *exclude* the configuration file from version control.ntfy("Job has been allocated, starting Model training")try:results=do_some_stuff()ifresults["metric"]>threshold:ntfy(f"Great model! Its metric is{results['metric']:.3f}",tags="white_check_mark",# this is the ✅ emojiclick=results["online_run_url"],)else:ntfy(f"Done, but not great ({results['metric']:.3f})",tags="disappointed")exceptExceptionase:ntfy.notify(f"Error! ->{str(e)}",priority=4,emails="you@foo.bar")Notentfy(message)is equivalent tontfy.notify(message), the former is an alias of the latter.If you do not receive notificationsafter you've made sure you've subscribed to the exact topic used by yourNotifier, it's probably because the request being sent out is malformed. Investigate usingdebug=Truein thenotify()call (e.g.:ntfy(message, debug=True)).User GuideThe central concept is"topics". It's basically an ID used to publish/subscribe to notifications. You should keep it secret and not easily guessable becauseanyone with the topic id can subscribe to your notifications. In particular, you should add.ntfy.confto.gitignore.In short,ntfy-wrapperwillpublishto atopicand you'll have tosubscribeto that same topic in order to receive the notification. You can receive your notification:On your computerBy opening alocal web appBy setting up thentfyCLI toolOn your phoneBy installinga mobile appHow to useCreate topics in one of the following ways:configure one manuallyIn a Python console usentfy_wrapper.utils.generate_topic()to get a secure unique and human-readable topic (eg:winter-abide-dinghy-wand)From the command-line use$ py-ntfy new-topicto get a similar topic. Add--saveto add it to the configuration.Tell theNotifierto use this topic in one of the following ways:In your codeNotifier(topics=your_topic)orNotifier(topics=[topic1, topic2])Using aconfiguration fileA configuration file will be created by default when constructing aNotifierexceptif you addwrite=Falseto theNotifier.__init__argumentsThe configuration file is used to hold default values for:targets, i.e. a list of comma-separatedtopicsand a list of comma-separatedemailsmessage_defaultswhich are default values used when calling.notify(...)You can also use$ py-ntfy initto initialize yourntfy-wrapperconfigurationFrom the command-line with$ py-ntfy add topic your-topicSetup defaults in one of the following waysBy editing the config fileBy using thepy-ntfycommand-line toolExplore notification options by referring to the originalntfydocsConfiguration filentfy-wrapperuses the INI standard along withconfigparserto parse the configuration file. It expects 2 sections:[notifier_init]with optional fieldsemails =,topics =andbase_url =to define systematic targets for the notification instead of putting them in your Python code.base_urlallows to push to a differentntfyserver than the defaulthttps://ntfy.sh.[notify_defaults]with optional fields listed below, which will define default parameters used byNotifier.notify(...). For instance you can set defaulttitleandtagsfor your code's.notify(...)calls and override them at specific locations with keyword arguments.notify(title="Non-default title")The behavior of thetitle,priority,tags,click,attach,actionsandiconkeys is described in thentfydocs# For Notifier(emails=..., topics=...)[notifier_init]topics=my-secret-topic-1, mysecrettopic2emails=you@foo.barbase_url=https://custom-ntfy-instance.io# For Notifier.notify(title=..., priority=..., etc.)[notify_defaults]title=Message from ntfy-wrapperpriority=0tags=fireclick=attach=actions=icon=https://raw.githubusercontent.com/vict0rsch/ntfy-wrapper/main/assets/logo.pngCommand-linentfy_wrappercomes with a command-line interface calledpy-ntfy. It uses the great Python CLI toolTyper. Its goal is to interact withntfy-wrapper's configuration in a user-friendly way. It is different in that sense to the originalntfyCLI toolwhich is more generic.Get help$py-ntfy--help
Usage:py-ntfy[OPTIONS]COMMAND[ARGS]...

╭─Options────────────────────────────────────────────────────────────────────────────────╮
│--install-completionInstallcompletionforthecurrentshell.│
│--show-completionShowcompletionforthecurrentshell,tocopyitor│
│customizetheinstallation.│
│--helpShowthismessageandexit.│
╰──────────────────────────────────────────────────────────────────────────────────────────╯
╭─Commands───────────────────────────────────────────────────────────────────────────────╮
│add[commandsub-group]Addanewnotificationtargetoradefaultnotification│
│value.Run`$py-ntfyadd--help`formoreinfo.│
│cleanRemovestheconfigurationfile.Use--conf-pathtospecifyapathtothe│
│configurationfile.Use--forcetoskiptheconfirmationprompt.│
│initInitializestheconfigurationfile.ItshouldNOTbetrackedbyversion│
│controlinordertoprotectthetopicID.Use--conf-pathtospecifyapath│
│totheconfigurationfile.Use--forcetooverwriteanexistingconfiguration│
│file.│
│new-topicGeneratesarandomtopicnameandsavesittotheconfigfileifyouusethe│
│--saveoption.│
│remove[commandsub-group]Removeanotificationtargetoradefaultnotification│
│value.Run`$py-ntfyremove--help`formoreinfo.│
│sendSendsanotificationtothegivenemailsandtopics.Optionalcommand-line│
│argumentscanbepassedtooverridethedefaultsintheconfigfileand│
│customizethemessageoptions.Refertohttps://ntfy.sh/docs/publishto│
│understandtheoptions.Run`py-ntfysend--help`toseetheavailable│
│options.│
╰──────────────────────────────────────────────────────────────────────────────────────────╯

$py-ntfyadd--help(similaroutput)$py-ntfyadddefault--help(similaroutput)$py-ntfyremove--help(similaroutput)Initialize the configuration file$py-ntfyinit
🔑Yourfirsttopicis`aloe-corset-stream-alto`.Useittosubscribetonotifications!
🎉Configfilecreatedat/path/to/repo/.ntfy.confAdd a topic or an email$py-ntfyaddtopicsome-secret-string-for-your-topic
🎉Topic`some-secret-string-for-your-topic`addedto/path/to/repo/.ntfy.conf

$py-ntfyaddemailyou@foo.bar
🎉Emailyou@foo.baraddedto/Users/.../vict0rsch/ntfy-wrapper/.ntfy.confAdd a default value for the.notify(...)calls$py-ntfyadddefaultkeyvalue
🎉Defaultkey=valueaddedto/Users/.../vict0rsch/ntfy-wrapper/.ntfy.confRemove items by simply replacingaddbyremove$py-ntfyremovedefaultkey
🎉Defaultkey=valueremovedfrom/Users/.../vict0rsch/ntfy-wrapper/.ntfy.conf

$py-ntfyremoveemailhello@you.com
Emailhello@you.comdoesnotexist.Ignoring.Generate a new topic withnew-topicand add it to your configuration with--save$py-ntfynew-topic--save
🎉Topicnutty-tiling-clear-parloraddedto/Users/.../vict0rsch/ntfy-wrapper/.ntfy.confSend a notification from the command-line withsend$py-ntfysend"hello"--topicsfrays-errant-acting-huddle--title"This is Victor"--click"https://9gag.com"🎉Notificationsenttofrays-errant-acting-huddle,you@foo.barChange the default configuration path for any command with the option--conf-pathSpecify a directory--conf-path path/to/conf/directoryand.ntfy.confwill be created thereSpecify a file--conf-path path/to/file.confand that will be used as a configuration fileTodoBetter readme and docCLIScreenshotsrequeststimeout or non-blocking[^1]: cfxkcd936