Table of contents
We've all been there - sitting at home, scrolling through social media, trying to find something to do. Boredom can strike at any time, and it can be tough to come up with ideas for activities that are both enjoyable and stimulating. This is where a boredom-busting app can come in handy.
By creating an app that suggests fun and creative activities to do when you're feeling bored, you can help users discover new hobbies, find inspiration, and enjoy their free time to the fullest. Whether it's suggesting a new recipe to try, a local hiking trail to explore, or a virtual museum tour to take, your app can provide endless ideas and entertainment options.
In this article, we'll explore the process of building an app that helps users beat boredom. Just a simple one using APIs.
Grab your supplies!
Preferred reading
Let's build it 🛠️
#01
Import modules
import streamlit as st
import requests
import json
import time
Streamlit is a powerful and user-friendly framework for building data applications in Python. With its simple syntax and intuitive interface, you can quickly create interactive dashboards, visualizations, and other data-driven tools.
Requests is a popular Python library for making HTTP requests, allowing you to send and receive data over the internet. Its simple and intuitive API makes it easy to integrate with other Python code and APIs.
JSON is a lightweight data interchange format that is widely used in web development and other applications. The json module in Python provides functions for encoding and decoding JSON data, making it easy to work with JSON strings and objects.
The time module in Python provides various functions for working with time-related data, including getting the current time, converting between different time formats, and adding or subtracting time intervals.
#02
Configure page setting
st.set_page_config(page_title = "Are you bored?", page_icon = "🦥")
#03
Intro
st.header("Are you bored?")
st.subheader("Let's find you something to do")
#04
API Endpoints
What is an API Endpoint mean? In a nutshell, An API endpoint is a specific URL that is used to access the data or resources of an API.
bored_api_url = "http://www.boredapi.com/api/activity/"
giphy_api_url = "https://giphy.p.rapidapi.com/v1/gifs/search"
#05
Secrets Management
Secrets management refers to the practices and technologies used to secure sensitive information, such as passwords, API keys, and other credentials. With the increasing amount of data being stored and transferred online, it's more important than ever to protect this information from unauthorized access.
gif_api_key = st.secrets["gif-api-key"]
headers = {
"X-RapidAPI-Key": st.secrets["X-RapidAPI-Key"],
"X-RapidAPI-Host": "giphy.p.rapidapi.com"
}
#06
Request
To produce some random result we will create a random button with the help of st.button()
function.
random = st.button("random", help="Click the button to generate an activity for you")
response = requests.request('GET', url)
result = response.text
data = json.loads(result)
Here is what the data from Bored API will look like:
#07
Complete Code
We can display the content by accessing it with the right key from the JSON data. Below is the complete code of our app. Once the user clicks the random button, it automatically sends the request to the Bored API. With that response, it then accesses the activity and passes it to the Giphy API to get the desired gif. Then with the help of streamlit's text and image element, the results are displayed. The code is pretty much self-explanatory. st.secr
import streamlit as st
import requests
import json
import time
# Set the title and icon of the Streamlit app
st.set_page_config(page_title="Are you bored?", page_icon="🦥")
# Add a header and subheader to the app
st.header("Are you bored?")
st.subheader("Let's find you something to do")
# Define the API URLs and API keys for the Bored API and GIPHY API
bored_api_url = "http://www.boredapi.com/api/activity/"
gif_api_url = "https://giphy.p.rapidapi.com/v1/gifs/search"
gif_api_key = st.secrets["gif-api-key"]
# Define the headers for the GIPHY API request
headers = {
"X-RapidAPI-Key": st.secrets["X-RapidAPI-Key"],
"X-RapidAPI-Host": "giphy.p.rapidapi.com"
}
# Create a "random" button to generate a new activity
random = st.button("random", help="Click the button to generate an activity for you")
# If the "random" button is clicked...
if random:
# Make a request to the Bored API and parse the response as JSON
response = requests.request('GET', bored_api_url)
result = response.text
data = json.loads(result)
# Extract the activity and type of activity from the Bored API response
activity = data['activity']
type_of_activity = data['type']
# Make a request to the GIPHY API with the activity as the search query
querystring = {"api_key": gif_api_key, "q": activity, "limit": "1"}
response_gif = requests.request("GET", gif_api_url, headers=headers, params=querystring)
result_gif = response_gif.text
data = json.loads(result_gif)
# Create a progress bar to show the user that the app is loading
my_bar = st.progress(0)
for percent_complete in range(100):
time.sleep(0.05)
my_bar.progress(percent_complete + 1)
# Display the activity and type of activity as a Markdown header and caption
st.markdown(f'### {activity}')
st.caption(f'Type: {type_of_activity}')
# If a GIF URL was found for the activity, display the GIF
# Otherwise, display a default "bored" GIF
if len(data["data"][0]["images"]["original"]["url"]) == None:
st.image("https://media.giphy.com/media/71WX8U8zZSBO75RARa/giphy.gif")
else:
st.image(data["data"][0]["images"]["original"]["url"])
st.secrets
is a feature of the Streamlit library that allows developers to store and retrieve sensitive information, such as API keys and credentials, without exposing them in the source code or version control system. Secrets are stored in a separate file named secrets.toml
that is excluded from the code repository. For more info, check Secrets - Secrets management.
The End😴
The final app will look like the one below. You can also find the live app here.
I hope you're feeling less bored now, and hopefully, even a bit inspired to try out some of the activities suggested by the app. If you're still feeling bored, well, there's always the "random" button to generate a new idea for you. Thanks for joining me on this quest to banish boredom! Now go out there and do something fun - or at least more interesting than reading about how to combat boredom. ;)