Ajinkya
Ajinkya

Reputation: 1887

Convert curl command from google-colab to python script

I have the following google colab code:

code :

!wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip

!unzip ngrok-stable-linux-amd64.zip

LOG_DIR = './log'
get_ipython().system_raw(
    'tensorboard --logdir {} --host 0.0.0.0 --port 6006 &'
    .format(LOG_DIR)
)

get_ipython().system_raw('./ngrok http 6006 &')    
! curl -s http://localhost:4040/api/tunnels | python3 -c \
    "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"

output: https://6a112ff8.ngrok.io

My question is How do I convert the curl pipe python command (last 3 lines) into a python script ? Currently it is being executed in google colab.

I have tried to get close to solution using this code :

import sys, json
import requests
from IPython import get_ipython


LOG_DIR = './log'

get_ipython().system_raw(
    'tensorboard --logdir {} --host 0.0.0.0 --port 6006 &'
    .format(LOG_DIR)
)

response = requests.get('http://localhost:4040/api/tunnels')
# result=json.load(response)
print(json.load(response)['tunnels'][0]['public_url'])

However I get an error:

AttributeError: 'NoneType' object has no attribute 'system_raw'

Upvotes: 2

Views: 7686

Answers (2)

Ajinkya
Ajinkya

Reputation: 1887

Thanks to @v25 comment I am able to run it using python. Here's what I understood.

Warning: On Macos there is some kind of permission issue with ngrok, hence I coudnt get the fist demo.py script running on macos. I got it running on Ubuntu 16

I downloaded these files and placed them in a folder where demo.py and demo2.py are present

wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip

unzip ngrok-stable-linux-amd64.zip

I made 2 scripts:

demo.py (nominal case)

import os
os.system('./ngrok http 8000 &')

for tensorboard replace os.system with:

os.system('tensorboard --logdir {} --host 0.0.0.0 --port 8001 &'.format(LOG_DIR))

demo.py will run in one terminal window.

demo2.py

import os
import requests

try:
    r = requests.get('http://localhost:4040/api/tunnels')
    d = r.json() 
    public_url = d['tunnels'][0]['public_url']
    print(public_url)
except Exception as e:
    print ('Failed: ', e)

demo2.py will run in another terminal window.

demo2.py will produce a url which I can use. Please refer to v25's comment to see in details why 2 seperate terminal windows are needed. I am adding this solution for my future reference.

Upvotes: 1

v25
v25

Reputation: 7631

! curl -s http://localhost:4040/api/tunnels | python3 -c \
    "import sys, json; print(json.load(sys.stdin)['tunnels'][0]['public_url'])"

To do this without curl in pure python, you could use the requests library, and the json method of the response object (r in this example) which returns a dictionary:

import requests
try:
    r = requests.get('http://localhost:4040/api/tunnels')
    d = r.json() 
    public_url = d['tunnels'][0]['public_url']
except Exception as e:
    print ('Failed: ', e)

# Do something with `public_url`

Upvotes: 1

Related Questions