Create an App to Deal with Boredom Using PyWebIO | by Khuyen Tran | Towards Data Science

Have you ever been so bored that you searched on Google: “What to do when being bored?” Wouldn’t it be nice if you can create an app that suggests users a random activity for the day and books related to that activity using Python?
You can play with the app shown above here. In this article, I will show you how to create this app in a few lines of code using PyWebIO, Bored API, and Open Library.
Before getting into the code, let’s collect the tools we need to create the app.
PyWebIO is a Python library that allows you to build simple web applications without the knowledge of HTML and Javascript. It is easier to learn than Django and Flask, but more customizable than Streamlit.
towardsdatascience.com
To install PyWebIO, type:
pip install pywebio
Bored API helps you find things to do when you’re bored. You should get a suggestion on an activity to do by simply going to https://www.boredapi.com/api/activity.
Refreshing the page will give you a different result:
If you prefer to engage in a certain type of activity, add ?type=activity-name to the API:
https://www.boredapi.com/api/activity?type=education
Output:
Open Library is an open and editable library catalog for every book ever published. Open Library offers a suite of APIs to help developers get up and running with their data.
You can get a JSON file of Open Library’s search results by simply going to:
https://openlibrary.org/search.json?title=friends+influence+people
Summary of the outputs:
Now that we gather the tools needed to create the app, let’s utilize these tools to create an app.
First, create a dropdown to show all activity types:
Details of the code above:
put_markdown : write markdown
select : create dropdown
start_server : start a web server and serve PyWebIO applications on it. debug=True tells the server to automatically reload when the code changes.
Normally, the input form of PyWebIO will be destroyed after a successful submission.
However, users might want to play with different inputs, so we need to find a way to prevent the input form from disappearing after successful submission. This can be easily done with pywebio.pin .
Explanations for the code above:
while True : Allows users to insert new input indefinitely
put_select : Similar to theselect widget, but is used by the pin widget function to detect the change in the input
pin_wait_change : Listens to a list of pin widgets. When the value of any widgets changes, the function returns with the name and value of the changed widget.
with use_scope('activity', clear=True) : Clear the last output when there is a new output within the scope
Output:
Next, we will get a random activity based on the activity type using Bored API. To do that, we will:
create a function namedget_activity_content that usesrequests.get to make the request to https://www.boredapi.com/api/activity .
pass the inputs we got from the dropdown bar to the function get_activity_content . If the input’s value is education , the API will be https://www.boredapi.com/api/activity?type=education .
Nice!
Suggesting activities to do is good, but not very resourceful. What if users want to learn more about the activity that they will be doing? That’s why we will also suggest to them some useful resources to read using Open Library.
Steps to get relevant books from Open Library :
Get the name of the activity: In the image below, the name of the activity is “Write a thank you letter to an influential person in your life”
Extract noun phrases from the activity’s name: A noun phrase is a group of two or more words headed by a noun such as “a beautiful girl”. We will use spaCy to extract noun phrases from a text:
$ pip install spacy$ python -m spacy download en_core_web_sm
['a thank', 'you', 'an influential person', 'your life']
Search for the title of a book using extracted noun phrases. For example, to search for books with titles related toyour life , we will add ?title=your+life to the API:
https://openlibrary.org/search.json?title=your+life
In the code below, we use the queries extracted from noun phrases to get the relevant books from Open Library:
Sample output of the first book:
Display a table of books’ titles and authors: A JSON output is boring. Let’s turn the JSON results into a table using pywebio.output.put_table :
Output:
Sweet! Since it can take a long time to request the books, we will also add put_loading to the code so that the users know that the website is still loading:
Result:
Nice!
Congratulations! You have just learned how to create an app using PyWebIO, Bored API, and Open Library. I hope this article will give you the motivation to create an interesting app using Python.
Since it takes only several lines of code to create an app using PyWebIO, why not give it a try?
Feel free to play with the source code of this article here:
build.pyweb.io
I like to write about basic data science concepts and play with different algorithms and data science tools. You could connect with me on LinkedIn and Twitter.
Star this repo if you want to check out the codes for all of the articles I have written. Follow me on Medium to stay informed with my latest data science articles like these: