How to Deploy Python Swagger API on Google Colab
Prerequisites:
- Gmail Account.
- Access to Google Colab Product.
- Python Code Implementation for Swagger API .
- Knowledge of Python.
The running working google colab solution file is provided with a sample swagger API code.
Download Now!
Let's begin now.
!npm install localtunnel
I am going to use network tunneling technique to allow public access to my custom created RESTful web APIs. Know that normally custom created APIs deployed or execute on local server machine for testing purpose and they can easily be exposed to public access temporarily through network tunneling technique for testing purpose only. Google colab itself is a remote server, so, anything that is created on it that involves URL sharing or exposure cannot be access without network tunneling technique because the default behavior is deployment over local server. So, when google colab act as local server, the local hosted URL needs to temporary expose as public for testing purpose. So, here I am using python "localtunnel" library to serve this purpose.
4) Next step is to get public IP of google colab notebook because it is required in some cases by "localtunnel" library to bypass the access password requirement. For this purpose use following line of code in a new notebook cell after your swagger API code i.e.
!wget -q -O - ipv4.icanhazip.com
5) Then add following line of code in a new cell at the end of the notebook i.e.
!python my_api.py & npx localtunnel
The above line of code not only just starts the server for your swagger API, but also expose the localhost URL to a randomly generated public URL, that you can use to test your RESTful web APIs. "my_api.py" is the file name where I have saved my swagger API code.
6) Time to execute the deployed Swagger RESTful APIs. Use any RESTED client and make sure to add the necessary headers, especially to bypass the localtunnel library password requirement as shown below. I have created a hello endpoint that simply returns JSON based "Hello World" message. Here's how the execution looks like in the RESTED client. i.e.
Conclusion
In this article, you will learn to deploy python swagger API on google colab for testing or staging purpose only.
You don't need to install any tool on your machine, just a gmail account
and access to google colab product. You will learn to generate public IP of your google colab notebook. You will also learn about the network tunneling technique to temporary expose localhost server publicly for testing purpose only. Finally, you will learn to deploy, execute and test out your swagger RESTful web API on any RESTED client with the help of google colab notebook as server without the need to create any complex docker or local deployments.