- Products
- Solutions Use casesBy industry
- Developers
- Resources Connect
- Pricing
Have you read our blog post, Shiny for Python: Building an email dashboard or watched our livestream Shiny for Python: Building an email dashboard but you’re looking for something else to build your dashboard using Python? How about an email dashboard using Streamlit?
We’re going to use Streamlit, a pure Python package that allows you to create data scripts into web apps in minutes.
If you don’t have the Nylas Python SDK installed and your environment isn’t configured, I would recommend you to read the post How to Send Emails with the Nylas Python SDK where everything is clearly explained.
Having the Nylas Python SDK installed and configured is important, but it’s not all we need. We need to install some additional packages, especially Streamlit:
$ pip3 install streamlit # Streamlit $ pip3 install matplotlib # Library for creating static, animated, and interactive visualizations $ pip3 install pandas as pd # Python data analysis library
Before moving on with the blog, we should take a look at what we are going to build, which is pretty much, a dashboard. This dashboard will use the Nylas Python SDK ability to access our email account and read the first hundred emails from our inbox and the first hundred emails from our sent folder.
With this information, we’re going to get the names of the 3 top people that we email from, the names of the top 3 people who we send emails to and finally, we’re going to generate a word cloud, using all the words gathered from the subjects of the emails in our inbox, that is, what are the most important words that people email us about.
Here we go:
We have a slider, with two bar plots and a word cloud.
First, we need to a file called streamlit_email_dashboard.py and enter the following code:
# Load your env variables from dotenv import load_dotenv load_dotenv() # Import your dependencies from nylas import APIClient import streamlit as st from wordcloud import WordCloud import os import pandas as pd import altair as alt import matplotlib.pyplot as plt # Initialize your Nylas API client nylas = APIClient( os.environ.get("CLIENT_ID"), os.environ.get("CLIENT_SECRET"), os.environ.get("ACCESS_TOKEN") ) # Auxiliar variables from_messages = [] to_messages = [] text = "" st.title('Email Dashboard') num = st.slider('How many emails?', 1, 200, 50) # Get emails from your inbox folder f_messages = nylas.messages.where(in_='inbox', limit = num) # Get emails from your sent folder t_messages = nylas.messages.where(in_='sent', limit = num) # Loop through your inbox emails for msg in f_messages: # Get the name of the person emailing you if(msg["from_"][0]["name"] != ""): from_messages.append(msg["from_"][0]["name"].split()[0]) # Concatate the subjects of all emails text = text + " " + msg["subject"] # Turn the array into a data frame f_df = pd.DataFrame(from_messages, columns=['Names']) # Aggregate values, get the top 3 and a name to the new column top_3_from = f_df["Names"].value_counts().head(3).reset_index(name="count") top_3_from.columns = ['person', 'count'] # Loop through your sent emails for msg in t_messages: # Get the name of the person you're emailing if(msg["to"][0]["name"] != ""): to_messages.append(msg["to"][0]["name"].split()[0]) # Turn the array into a data frame t_df = pd.DataFrame(to_messages, columns=['Names']) # Aggregate values, get the top 3 and a name to the new column top_3_to = t_df["Names"].value_counts().head(3).reset_index(name="count") top_3_to.columns = ['person', 'count'] # Using all the email subjects, generate a wordcloud wordcloud = WordCloud(width=800, height=300).generate(text) # Create columns layout col1, col2 = st.columns(2) with col1: # Create barchart from emails bar_chart = alt.Chart(top_3_from).mark_bar().encode( alt.X('person', title='person'), alt.Y('count', title='count'), color='person:N' ).properties( title='From: Emails' ) # Display the barchart st.altair_chart(bar_chart, use_container_width=True) with col2: # Create barchart to emails bar_chart = alt.Chart(top_3_to).mark_bar().encode( alt.X('person', title='person'), alt.Y('count', title='count'), color='person:N' ).properties( title='To: Emails' ) # Display the barchart st.altair_chart(bar_chart, use_container_width=True) # Display the generated image: fig, ax = plt.subplots(figsize = (12, 8)) ax.imshow(wordcloud) plt.axis("off") st.pyplot(fig)
To run it, we can simply go to the terminal and type:
$ streamlit run streamlit_email_dashboard.py
Our application will be running on port 8510 of localhost, so we just need to wait until it opens itself or open our favourite browser and go to the following address:
http://localhost:8510
We had easily and elegantly developed our Email Dashboard using Python and Streamlit.
To learn more about our Email APIs, please go to our documentation Email API Overview.
You can sign up Nylas for free and start building!
Don’t miss the action, join our LiveStream Coding with Nylas:
Blag aka Alvaro Tejada Galindo is a Senior Developer Advocate at Nylas. He loves learning about programming and sharing knowledge with the community. When he’s not coding, he’s spending time with his wife, daughter and son. He loves Punk Music and reading all sorts of books.