In a previous post, we built a simple Streamlit app to retrieve stock quote information, and walked through the steps to host it on Streamlit Cloud as well as Railway. In this post, we'll explore a third hosting option - Google Colab.
What is Google Colab?
Google Colaboratory (Colab for short), is a cloud-based platform for data analysis and machine learning. It provides a free Jupyter notebook environment that allows users to write and execute Python code in their web browser, with no setup or configuration required. Google Colab also provides access to powerful hardware resources, including GPUs and TPUs, making it a popular choice for training deep learning models. Additionally, Colab allows users to easily collaborate with others on projects by sharing notebooks and code. Colab notebooks are typically stored in Google Drive, or can be loaded directly from GitHub.
Run a Streamlit App on Google Colab Notebook
For this tutorial, I'll use my streamlit-yfinance GitHub repository. Launch Google Colab, and create a new notebook. There are broadly two types of cells in a notebook -
Text cells and
Code cells. Use the text cells for comments, and the code cells for your Python code. You can also use code cells to run commands e.g. installation of packages or running binaries as below.
!pip install -q streamlit
Create a Streamlit app and save to a local file
%%writefile app.py import streamlit as st import yfinance as yf st.title("Stocks App") symbol = st.text_input("Enter a stock symbol", "AAPL") if st.button("Get Quote"): st.json(yf.Ticker(symbol).info)
Install localtunnel to serve the Streamlit app
!npm install localtunnel
Run the Streamlit app in the background
!streamlit run app.py &>/content/logs.txt &
Expose the Streamlit app on port 8501
!npx localtunnel --port 8501
If you wish, you can skip the notebook creation and simply launch my notebook using the one-click button below instead.
Either way, once the notebook is ready, click on the play button next to each cell to execute the code within it. Once all the cells execute successfully, the Streamlit app will be available on a
***.loca.lt URL - click to launch the app. That's it! Google Colab is a powerful yet easy way to experiment with Python apps, and I'll explore more of it for machine learning use cases in the future.