Sharing a Taipy Application on Colab with Ngrok

In this article, we will explain how to deploy and share a Taipy Application on Colab (Google Notebook platform hosting) using a public URL created with Ngrok.

Problem statement

For those interested in developing powerful graphical interfaces without having to be a GUI specialist, Taipy is undoubtedly the answer.
Besides developing with Python scripts, Taipy is also completely adapted to Notebook environments. You can use Notebooks locally and through platforms such as:

In this article, we deploy a Taipy application on Notebooks hosted on the Google Colab platform using a public URL created with Ngrok.

Taipy on Google Colab with Ngrok

First, let’s start with a quick explanation of Ngrok and Google Colab.

What is Google Colab?

Google Colab is the Data Scientist’s favorite tool. It provides a Notebook environment on steroids. It gives you access to high-performance hardware (CPU & GPU), which is necessary for deep learning, without having to invest in physical machines.
Google Colab isn’t hosted locally but is cloud-based, meaning that you won’t be able to visualize the GUI of your web application directly. No worries, we will see how Ngrok can solve this problem.

What is Ngrok?

Ngrok allows you to share your application directly on the Internet. It creates a tunnel from your local machine to the outside world via a public URL.
This URL is temporary with the free version and can be permanent with a payed solution.

Setting up your Taipy application on Google Colab

Now let’s get started:

Our Taipy Example: A Sentiment Analysis Application
Let’s use a sentiment analysis application from Taipy’s Getting Started. This application displays multiple pages with a rich GUI.

Step 1: Getting Started with Taipy and Google Colab

Copy the Taipy Sentiment Analysis code into a Google Colab Notebook. This code generates a sentiment analysis web application using Taipy.
You can find the full code in this GitHub repository.

Executing the Taipy code (Note the instruction: Gui.run(...) which spins off a web server), launches a web app with its local URL: it is unaccessible from the Internet. As previously stated, Colab is hosted on a server and not locally on the machine.

Step 2: Getting your Authtoken on Ngrok

On the Ngrok website (create a hyperlink to Ngrok website), create a free account.

Ngrok home page

Once your account is created, you can access your personnal Authtoken. We need this Authtoken to create the tunnel for our Taipy application.

Ngrok authtoken page

Head to Ngrok website and obtain your tunnel Authtoken.

Sharing our Taipy application in Google Colab with Ngrok

The first step is importing Ngrok. Let’s install the Ngrok Python wrapper pyngrok

pip install pyngrok
The next step takes place in the Gui.run() call. Taipy has an in-built Ngrok Authtoken reader. We just have to add the ngrok_token parameter in the run() function and put the Authtoken provided by Ngrok.
Gui(pages=pages).run(ngrok_token="TaIpY19D3Pl0YaPp07C0laB199NgR0k93TaIpY19D3Pl0")

Running this final step generates a Ngrok public URL in the terminal.

Taipy GUI run with ngrok token
Taipy GUI trace with ngrok token

Let’s click on the Ngrok Public URL link to access our application.

Taipy Analyse with Ngrok

You have now succeeded to create and launch our multipage Taipy application from Colab!!
You can insert a word or sentence on the first page and look at their scores based on an NLP algorithm. The results are showcased in a table and a graph. On the second page, you can upload a file and observe the analysis of an entire text.
To know more, check out our Getting Started page.
You can also share this application with anyone on the Internet.

Update the delay parameter

Our application, opened via Ngrok, is refreshed according to a delay parameter. When we type in a word or sentence, we can observe that it is sometimes not correctly taken into account as the page is refreshed multiple times. That is due to the latency time over the Internet.
To counteract this, change the change_delay parameter either:

  • Locally:
    We can update this parameter locally i.e. directly on the Taipy visual element.
page = """
# Getting started with Taipy GUI
<|layout|columns=1 1|
<|
My text: <|{text}|>
Enter a word:
<|{text}|input|change_delay=800|>
  • Or globally:
    To update the delay for all of Taipy’s visual elements:
Gui(pages=pages).run(change_delay=800,
ngrok_token="TaIpY19D3Pl0YaPp07C0laB199NgR0k93TaIpY19D3Pl0")

Reloading after modification

In its current state, if you make changes to a cell, there’s no requirement to restart the kernel. You can easily re-run the Notebook to see your updates take effect. However, in Ngrok’sfree version, you are limited to three re-executions, which necessitates restarting the kernel.

Nonetheless, Taipy includes built-in functions that enhance the notebook experience with Taipy. This eliminates the limitations of the ngrok free version and allows for easy updates with minimal re-executions! Check out this article to know more about it.

Modification of the markdown with Page.set_content

Here are the new cells to add:

Import Markdown

from taipy.gui import Gui, Markdown

Set empty new page

new_page=Markdown("")
Set content to new_page
new_page.set_content(page)
Update the pages definition
pages = {"/":"<|toggle|theme|>\n<center>\n<|navbar|>\n</center>",
         "line":new_page,
         "text":page_file}

Variable modification with gui.reload

Add this step

gui=Gui(pages=pages)
Update your Gui.run
gui.run()
Add the gui.reload function
gui.reload()

Once you’ve made your modifications, simply rerun the changed cell and trigger the reload function. Refresh your application page to see your updates.

Conclusion

In conclusion, deploying a Taipy application on Google Colab with Ngrok allows for easy sharing of graphical interfaces and applications on the Internet.
By using Google Colab, you gain access to high-performance hardware without having to invest in physical machines. Meanwhile, Ngrok provides a temporary or permanent public URL for your application, making it accessible to anyone. By following the steps outlined in this article, you can easily set up your Taipy application on Google Colab and share it with others. Additionally, updating the delay parameter can help improve the user experience for your application. Overall, this approach is a great option for those who want to develop and deploy powerful GUIs in the Cloud without too much complexity.