Shiny for Python: Building an Email Dashboard

From the R world into the Python world. Learn how to use the Shiny Web Framework to create an email dashboard.

Shiny for Python: Building an Email Dashboard

Have you ever used Shiny? Shiny is an R package that allows you to create interactive web applications that can be used as standalone web applications, embedded in R Markdown or in dashboards.

But we’re not here today to talk about R, but to talk about Python. And now Python has access to Shiny, which is wonderful news. By using Shiny for Python, we can create amazing dashboards, using all the power that Python has to offer.

Let’s create an Email Dashboard using Shiny for Python.

What are we going to do today?

  • Is your system ready?
  • Installing additional packages
  • What are we going to build?
  • What does our dashboard look like?
  • Let’s start building

Is your system ready?

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.

Installing additional packages

Having the Nylas Python SDK installed and configured is important, but it’s not all we need for sure. We need to install some additional packages, especially Shiny for Python.

$ pip3 install shiny # Shiny for Python
$ pip3 install seaborn # Python data visualization library
$ pip3 install pandas # Data analysis library
$ pip3 install wordcloud # Wordcloud generator
$ pip3 install matplotlib # library for creating static, animated, and interactive visualizations

What are we going to build?

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 take advantage of the Nylas Python SDK ability to access our email account and read the first 100 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.

What does our dashboard look like?

Here we go:

Shiny for Python dashboard

We have two bar plots and one word cloud. I wished the word cloud image was centered, but haven’t found a way yet.

Let’s start building

First, we need to head to our terminal window and type the following:

$ shiny create mail_dashboard

This will create a file called that we’re going to update with our own source code:

# Load your env variables
from dotenv import load_dotenv

# Import your dependencies
from shiny import App, render, ui
from shiny.types import ImgData
import matplotlib.pyplot as plt
import pandas as pd
from nylas import APIClient
import os
import seaborn as sns
from wordcloud import WordCloud

# Initialize your Nylas API client
nylas = APIClient(

# Auxiliar variables
from_messages = []
to_messages = []
text = ""

# Get emails from your inbox folder
f_messages = nylas.messages.where(in_='inbox', limit = 100)
# Get emails from your sent folder
t_messages = nylas.messages.where(in_='sent', limit = 100)

# Loop through your inbox emails
for msg in f_messages:
	# Get the name of the person emailing you
	if(msg["from_"][0]["name"] != ""):
	# 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"] != ""):	
# 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)
# Download the wordcloud image

# Create the dashboard layout
app_ui = ui.page_fluid(
        .app-col {
            border: 1px solid black;
            border-radius: 5px;
            background-color: #eee;
            padding: 8px;
            margin-top: 5px;
            margin-bottom: 5px;
    ui.h2({"style": "text-align: center;"}, "Email Dashboard"),
                {"class": "app-col"},
               ui.h2({"style": "text-align: center;"},"From: Emails"),
                {"class": "app-col"},
                ui.h2({"style": "text-align: center;"},"To: Emails"),
                {"class": "app-col"},
                ui.h2({"style": "text-align: center;"},"Email Subject's Wordcloud"),

def server(input, output, session):
# Display the "From" emails	
	def _from_():
		g = sns.catplot(data=top_3_from,x="person",
		return g
# Display the "To" emails
	def _to_():
		h = sns.catplot(data=top_3_to,x="person",
		return h		
# Display the Wordcloud	
	def image():
		from pathlib import Path
		dir = Path(__file__).resolve().parent
		img: ImgData = {"src": str(dir / "wc_email.png"), "width": "800px",       
		return img

# Start the server
app = App(app_ui, server, debug=True)

To run it, we can simply go to the terminal and type:

$ shiny run --reload mail_dashboard/

Our application will be running on port 5000 of localhost, so we just need to open our favorite browser and go to the following address:


If you want to learn more about our Email APIs, please go to our documentation Email API Overview.

Don’t miss the action, join our LiveStream Coding with Nylas:

You May Also Like

Best Salesforce email integrations
Find the best Salesforce email integrations for your business
Nylas v2 EOL
Signing off Nylas API v2: Welcome Nylas API v3
Capture all customer email activity in Salesforce to drive GTM alignment

Subscribe for our updates

Please enter your email address and receive the latest updates.