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
- 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
- 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.")
- 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:")
- 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
- 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.")
- Run the App Save the file and run the Streamlit app with the following command in the terminal:
streamlit run word_cloud_app.py
- 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](https://www.vskills.in/certification/tutorial/wp-content/uploads/2024/12/Certificate-in-Nessus-Scanner-banner-1.png)