Streamlit App for Word Clouds

Learn how to build a complete web app using Streamlit.

Load the data

Before writing any code for the Streamlit app, we need to load the data from our JSON files.

def load_data():
    with open('data/weekly.json','r') as file:
         weekly_keywords = json.load(file)
    with open('data/combined.json') as file:
         combined_keyword = json.load(file)
    dates = [date for date in weekly_keywords]
return combined_keyword,weekly_keywords,dates

Select the image mask

We’ll also return all the dates for which we collected the data.

st.title("2020 Word Clouds based on Google Keyword and Twitter Hashtag trends")
image = st.sidebar.selectbox(label='Select Image Mask',options=
['default','twitter','hashtag','heart'])
combined_keyword,weekly_keywords,dates = load_data()

A sidebar with a drop-down menu will be created for the user to select the image mask they want to use. For the 2020 word cloud, we’ll set the maximum number of words to 800 and maximum font size to 15.

st.header("Entire Year")
wordcloud = get_word_cloud(image,combined_keyword,800,15)
fig1 = plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
st.pyplot(fig1)

Select the date

For the weekly word cloud, we can increase the font size because we don’t have many unique words. We’ll also create a drop-down menu for the user to select a date.

st.header("Weekly")
date = st.selectbox(label='Select Date',options=dates)
keywords = weekly_keywords[date]
wordcloud = get_word_cloud(image , keywords,200,25)
fig2 = plt.figure()
plt.imshow(wordcloud)
plt.axis("off")
st.pyplot(fig2)

Let’s run the complete Streamlit web app to generate and display word clouds.

Get hands-on with 1200+ tech skills courses.