简体   繁体   中英

How to run different Jupyter notebooks conditionally in a AWS Sagemaker notebook instance

I have a AWS sagemaker notebook instance which have 2 different jupyter notebooks. There are certain conditions in which each of it should work.

So, if the consition A exist, the Jupyter Notebook 1 should run and if Condition B exist, the Jupyter Notebook 2 should run.

I have tried this code so far, but it doesnt work:

if condition A:

    sm_client = boto3.client('sagemaker')
    notebook_instance_name = NotebookInstanceName
    notebook_instance_name = 'Calorie-1615128222'
    url = sm_client.create_presigned_notebook_instance_url(NotebookInstanceName=notebook_instance_name)['AuthorizedUrl']
    
    print(url)

    url_tokens = url.split('/')
    http_proto = url_tokens[0]
    http_hn = url_tokens[2].split('?')[0].split('#')[0]

    s = requests.Session()
    r = s.get(url)
    cookies = "; ".join(key + "=" + value for key, value in s.cookies.items())
    print(cookies)

    ws = websocket.create_connection(
        "wss://{}/terminals/websocket/1".format(http_hn),
        cookie=cookies,
        host=http_hn,
        origin=http_proto + "//" + http_hn
    )
    
    print(ws)
    
    # ws = websockets.connect("wss://{}/terminals/websocket/1".format(http_hn))
    
    ws.send("""[ "stdin", "jupyter nbconvert --execute --to notebook --inplace /home/ec2-user/SageMaker/Calorie/Notebook1.ipynb 
--ExecutePreprocessor.kernel_name=conda_tensorflow2_p36 --ExecutePreprocessor.timeout=1500\\r" ]""")
    #ws.send("""[ "stdin", "jupyter nbconvert --execute Notebook1.ipynb --ExecutePreprocessor.kernel_name=conda_tensorflow2_p36 --ExecutePreprocessor.timeout=1500\\r" ]""")
    
    time.sleep(5)
    ws.close()
    print("websocket client created")
    #return None
    

Please help. Many Thanks

Have you considered orchestrating this using AWS Step Functions or SageMaker Pipelines depending on your use case?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM