# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# nuclio: ignore
import nuclio

# %nuclio config kind = "job"
# %nuclio config spec.image = "python:3.6-jessie"

# %%nuclio cmd -c 
pip install requests

import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)

import os
import json
import requests
from mlrun.execution import MLClientCtx
from typing import List


def slack_notify(
    context: MLClientCtx,
    webhook_url: str = "URL",
    slack_blocks:  List[str] = [],
    notification_text: str = "Notification"
) -> None:
    """Summarize a table
    :param context:         the function context
    :param webhook_url:     Slack incoming webhook URL. Please read: https://api.slack.com/messaging/webhooks
    :param notification_text:            Notification text
    :param slack_blocks:          Message blocks list. NOT IMPLEMENTED YET
    """
    
    data = {
        'text': notification_text
    }
    print("====",webhook_url)
    response = requests.post(webhook_url, data=json.dumps(
        data), headers={'Content-Type': 'application/json'})

    print('Response: ' + str(response.text))
    print('Response code: ' + str(response.status_code))


# +
# nuclio: end-code
# -

# ### mlconfig

# +
from mlrun import mlconf
import os

mlconf.dbpath = 'http://mlrun-api:8080'
mlconf.artifact_path = mlconf.artifact_path or f'{os.environ["HOME"]}/artifacts'
# -

# ### save

# +
from mlrun import code_to_function

# create job function object from notebook code
fn = code_to_function("slack_notify")
# add metadata (for templates and reuse)
fn.spec.default_handler = "slack_notify"
fn.spec.description = "Send Slack notification"
fn.metadata.categories = ["ops"]
fn.metadata.labels = {"author": "mdl"}
fn.export("function.yaml")
# -

# ## tests

from mlrun import import_function
func = import_function("hub://slack_notify")

# +
from mlrun import NewTask, run_local


#Slack incoming webhook URL. Please read: https://api.slack.com/messaging/webhooks
task_params = {
    "webhook_url" : "<KEY>",
    "notification_text" : "Test Notification"
}
# -

task = NewTask(
    name="tasks slack notify", 
    params = task_params,
    handler=slack_notify)

# ### run local where artifact path is fixed 

run = run_local(task, artifact_path=mlconf.artifact_path)

# ### run remote where artifact path includes the run id

func.deploy()

func.run(task, params=task_params,  workdir=mlconf.artifact_path)

func.doc()



StarcoderdataJupyterScriptsDedupFiltered