GithubHelp home page GithubHelp logo

leonardloo / codealert Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 457 KB

Slack/Email/Sound alert when your code block finishes running

JavaScript 14.08% HTML 1.37% Python 47.04% Jupyter Notebook 37.51%

codealert's Introduction

CodeAlert

Slack/Email/Sound alert when your code block finishes running.

Requirements

Python 2/3

Installation

  • Using pip: pip install codealert

  • Manually: Download python/codealert/ folder, and import it in your Python project.

Usage

Check out python/CodeAlert.ipynb for working examples.

Setup and configuration

# Import after pip install codealert
from codealert import *

# Initialize
codealert = CodeAlert()

# Easy way to turn it on/off
codealert.set('on', True)

# Method chaining way to set attributes. Sound and email is enabled but Slack is not.
codealert.set('sound_enabled', True).set('email_enabled', True).set('emails', ['YOUR_EMAIL'])

List of attributes:

  • on: bool
  • sound_enabled: bool or a str indicating the sound you want to be played, i.e. passing in done will invoke a terminal sound saying done.
  • email_enabled: bool
  • emails: [] array of string emails to send to
  • slack_enabled: bool
  • slack_urls: [] array of slack urls to send to. Please obtain your slack urls from https://my.slack.com/services/new/incoming-webhook/
  • logtext: str default log text that is sent to your email/Slack. If ping is being called with a str argument passed in, that will override logtext. print statements in a pingd decorated function will also override logtext.

In-line alerts

# Start Code block
import time
time.sleep(3)
# End Code block

# One line ping here that will send you an email and emit a sound
codealert.ping('Finished sleeping for 3 seconds')

If you want to enable Slack, you'll have to reconfigure your codealert instance:

slack_urls = ['https://hooks.slack.com/services/T---/B---/---']
codealert.set('slack_enabled', True).set('slack_urls', slack_urls)

Then ping to send you a notification in Slack:

import time
time.sleep(1)

codealert.ping("CodeAlert pinging Slack after 1 second")

Decorate functions

pingd(codealert, options) decorator that takes in two optional arguments: codealert instance and options which is a dictionary of attributes that applies only to this function. If codealert is not passed in, a new codealert instance will be initialised. If options is not passed in, options set in codealert will be used.

Note: options does not mutate codealert instance passed in.

custom_options = {'email_enabled': False, 'sound_enabled': 'done', 'slack_enabled': True}

# Pass in code alert and custom options
@pingd(codealert, custom_options)
def func(a, b, c):
    import time
    time.sleep(3)
    
# Test call function
func('a', 'b', 'c')

This will send you a Slack notification and play a sound after the end of the function execution.

Decorated functions also sends you the ping whenever an error happens in the function. Furthermore, all print statements in a function will be forwarded to you, along with the error invoked:

# 
@pingd(codealert)
def func_error():
    import time
    time.sleep(3)
    print('This code is before the error')
    x = None + 2
    print('This code does not print')
    
# Test call function
func_error()

The above function when called will send to your Slack the following message:

Error:
======
unsupported operand type(s) for +: 'NoneType' and 'int'

Print:
======
This code is before the error

Important things to note when using

  • I have hosted a small server to handle email notifications. Hence if you are using email notifications, please do not spam my server, i.e. do not place a ping() call inside a for loop. Violation of this will result in me banning your usage of the server for email notifications permanently.

For open source contributors

  • Please commit to a separate branch and submit a pull request.
  • If you run your own node.js server, remember to change the BASE_URL in CodeAlert.py to your server's host and port.
  • If you want to run email alerts, you have to create a credentials.txt in the root directory of your node.js app. The first line of the file will be your email address, and the second line being your email pass, i.e.

codealert's People

Contributors

maseh87 avatar leonardloo avatar

Watchers

James Cloos avatar  avatar Wing Yiu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.