execute_cli 627 B

1234567891011121314151617181920212223242526
  1. #!/usr/bin/env python3
  2. import os
  3. import sys
  4. import time
  5. import requests
  6. # Read the Python code from STDIN
  7. code = sys.stdin.read()
  8. # Set the default kernel ID
  9. kernel_id = 'default'
  10. # try 5 times until success
  11. PORT = os.environ.get('JUPYTER_EXEC_SERVER_PORT')
  12. POST_URL = f'http://localhost:{PORT}/execute'
  13. for i in range(5):
  14. response = requests.post(POST_URL, json={'kernel_id': kernel_id, 'code': code})
  15. # if "500: Internal Server Error" is not in the response, break the loop
  16. if '500: Internal Server Error' not in response.text:
  17. break
  18. time.sleep(1)
  19. # Print the response
  20. print(str(response.text))