[PYTHON] Notify Slack when the machine learning process running on GCP is finished

table of contents

  1. Overview
  2. Set up Incoming Webhooks in Slack
  3. Try posting to the URL of the Incoming Webhook
  4. Try to notify Slack with execution time

1. Overview

It takes hours to process machine learning at Kaggle competitions. .. If you have a high-spec VM, the monthly fee will be quite high, so please let us know in order to stop the VM or perform another process when the process is finished! Here's how to notify Slack of processing status!

2. Set up Incoming Webhooks in Slack

Access Slack's Incoming Webhook settings screen: [Slack] Incoming WebHooks

Screen Shot 2020-02-21 at 13.38.00.png

Select a channel to notify

Screen Shot 2020-02-21 at 13.38.48.png

Click "Add Incoming Webhooks integration"

Screen Shot 2020-02-21 at 13.39.27.png

The red frame below is the URL of the Incoming Webhook, so copy it.

Screen Shot 2020-02-21 at 12.45.57.png

Rename for Slack notifications: Optional

Screen Shot 2020-02-21 at 12.47.33.png

Changed icon for Slack notifications: Optional

Screen Shot 2020-02-21 at 12.49.30.png

If there is no problem, click "Save Settings"

Screen Shot 2020-02-21 at 12.50.23.png

3. Try posting to the URL of the Incoming Webhook

Install slack web

Terminal


pip install slackweb

** Example of slack web installation on Jupyter Lab on GCP ** Screen Shot 2020-02-21 at 12.52.59.png Screen Shot 2020-02-21 at 12.54.05.png

Try Post

#slackweb module import
import slackweb

#Specifying Slack's Incoming Webhook
slack = slackweb.Slack(url="URL of Slack's Incoming Webhook") #Rewrite it to your own URL

#Send with comment
slack.notify(text="It's a test")

** Example on Jupyter Lab on GCP ** Screen Shot 2020-02-21 at 12.55.04.png

Screen Shot 2020-02-21 at 12.59.37.png

Run!

It's definitely coming in real time! ezgif-2-8d87a76250d3.gif

4. Try to notify Slack with execution time

#Import of required module
import slackweb
import datetime

#Classify Slack notifications
class Slack_notice():
    start = 0
    
    def __init__(self):
        self.slack = slackweb.Slack(url="URL of Slack's Incoming Webhook") #Rewrite it to your own URL
        
    def begin(self):
        self.start = datetime.datetime.now()
        self.slack.notify(text='[Notice] ' + str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M")) + '\n Processing has started!')

    def end(self):
        elapsed_time = datetime.datetime.now() - self.start
        self.slack.notify(text='[Notice] ' + str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M")) + '\n Processing is finished! \n Time required'+ str(elapsed_time) + 'is')

# Slack_notice class call
slack_notice = Slack_notice()

#Pre-execution notification
slack_notice.begin()

#The process you want to execute-------------------------------------------------------------------------
#This time, for the sake of clarity, I just printed every second.
from time import sleep

for num in range(10):
    print(num)
    sleep(1)
# -------------------------------------------------------------------------------------

#Pre-execution notification
slack_notice.end()

** Example on Jupyter Lab on GCP ** ezgif-2-b0aa5ed4f4ae.gif

reference

[Qiita] Post to slack with Python 3

[Qiita] Let's easily make a screen video to upload to Qiita with gif animation

Recommended Posts

Notify Slack when the machine learning process running on GCP is finished
Automatically stop the VM when the machine learning process running on GCP is finished
Write a script in Shell and Python to notify you in Slack when the process is finished
Notes on running Azure Machine Learning locally
Notify Slack when the linux command finishes
[Machine learning] What is the LP norm?
Notify Slack of how Keras is learning
Notify slack when the switch sales page is updated ~ slack bot development with python ③ ~
Install the machine learning library TensorFlow on fedora23
Machine learning kit numpy matplotlib scipy There is a turn to follow when installing the three families on debian ubuntu
Let's use AWS Lambda to create a mechanism to notify slack when the value monitored by CloudWatch is exceeded on Python
What is machine learning?
Use Heroku in python to notify Slack when a specific word is muttered on Twitter
Survey on the use of machine learning in real services
I installed the automatic machine learning library auto-sklearn on centos7
[GCP] A memorandum when running a Python program on Cloud Functions
Build Python environment on Ubuntu (when pip is not the default)
Notes on installing Ubuntu 18.04 on the XPS 15 7590
Notes on running Azure Machine Learning locally
First Deep Learning ~ Struggle ~
First Deep Learning ~ Preparation ~
Notes on installing PycURL
First Deep Learning ~ Solution ~
The first thing to do after installing CentOS Stream minimally
Notes on installing dlib on mac
Deep running 2 Tuning of deep learning
Notes on installing pipenv on Mac
Notes on installing Anaconda 3 on Windows
Notes on installing Python on CentOS
Notes on installing Python using PyEnv
Notes on using matplotlib on the server
Notify Slack when the machine learning process running on GCP is finished