Creating a ‘Word Cloud’ Using Streamlit

A word cloud is a visualization of text data where the size of each word represents its frequency or importance. Streamlit is a Python library that makes it easy to build interactive web applications for such visualizations. Here’s a simple guide to creating a word cloud using Streamlit.

Prerequisites

Before starting, ensure you have installed Python and the required libraries. Install them using the following commands:

pip install streamlit wordcloud matplotlib

Step-by-Step Guide

  1. Import Necessary Libraries Start by importing the libraries required for the application:
    • import streamlit as st from wordcloud import WordCloud import matplotlib.pyplot as plt
  2. Set Up the Streamlit App Create a new Python file (e.g., word_cloud_app.py) and initialize a Streamlit app with a title:
    • st.title("Word Cloud Generator") st.write("Enter text to generate a word cloud visualization.")
  3. Input Text Data Use a text input box for users to provide the text that will be used to create the word cloud:
    • user_input = st.text_area("Paste your text here:")
  4. Generate the Word Cloud Write a function to generate the word cloud using the WordCloud library:
    • def generate_word_cloud(text): wordcloud = WordCloud(width=800, height=400, background_color="white").generate(text) return wordcloud
  5. Display the Word Cloud Use matplotlib to display the generated word cloud in the Streamlit app:
    • if user_input: wordcloud = generate_word_cloud(user_input) fig, ax = plt.subplots(figsize=(10, 5)) ax.imshow(wordcloud, interpolation="bilinear") ax.axis("off") st.pyplot(fig) else: st.write("Enter some text to generate a word cloud.")
  6. Run the App Save the file and run the Streamlit app with the following command in the terminal:
    • streamlit run word_cloud_app.py
  7. Test the App Open the provided URL in your browser, paste some text in the input box, and see the word cloud visualization.

Example Code

Here’s the complete code for the app:

import streamlit as st
from wordcloud import WordCloud
import matplotlib.pyplot as plt

st.title("Word Cloud Generator")
st.write("Enter text to generate a word cloud visualization.")

user_input = st.text_area("Paste your text here:")

def generate_word_cloud(text):
wordcloud = WordCloud(width=800, height=400, background_color="white").generate(text)
return wordcloud

if user_input:
wordcloud = generate_word_cloud(user_input)
fig, ax = plt.subplots(figsize=(10, 5))
ax.imshow(wordcloud, interpolation="bilinear")
ax.axis("off")
st.pyplot(fig)
else:
st.write("Enter some text to generate a word cloud.")

Conclusion

This simple app lets you quickly generate and visualize word clouds from any text data. It is a great way to explore text patterns and engage users interactively.

streamlit
Build a Machine Learning Model Using ChatGPT
Creating a Summary of Blog Post

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?