SoFunction
Updated on 2024-11-12

Sample code for a Python implementation to build a dashboard

This will save our team hours of repetitive data processing on a daily basis ......

synopsis

If you currently work in a data or business intelligence team, one of your tasks may be to produce some daily, weekly or monthly reports.

While obtaining these reports is not difficult, it still takes quite a bit of time. Our valuable time should be spent on more difficult tasks such as training neural networks or building data pipeline architectures.

So for these mundane and repetitive reports, the best way to save us time is to create a web application where other teams can access and download the reports themselves.

I'm not talking about paid tools like Tableau or PowerBI (which you can use if your company is on a budget). There are advanced web frameworks like Flask and Django that are commonly used to build a functioning website.

However, for a quick web dashboard to report metrics and numbers, you might consider using theStreamlit, which is a relatively new web framework built for ML practitioners and data science teams. It is very simple and intuitive to use.

element

I'll walk you through an example of cluster analysis using aHow to buildcap (a poem)deploymentsIt.

For each section, I will present acode template(which you can reuse in your own projects) and my code (for the queue analysis example used in this article).

  • Create a Python file
  • Run the file in a terminal to see the dashboard on the local machine
  • Deploy to other teams

1. Create a Python file

We have to create a Python file, which we can later call from the terminal to display the results on the browser.

You can name this file whatever you want. Here I'll call it

code template

# These data can be obtained by contacting the author by replying to [Cloudsun] in the public backend.
# 1. Import the necessary libraries
import pandas as pd
import numpy as np
import streamlit as st

# 2. Setup Page Configuration
st.set_page_config(
    page_title="This is my title",  # Title of the page
    page_icon="📈",                  # favicon
    layout="wide",
)

# 3. Define all the functions used in your project
# 4. for each function, add "@st.experimental_memo" in front of it. This is
# to memorize the execution of each function. This will make the application run faster.
# Especially when the user interacts with some of the elements on the dashboard #

@st.experimental_memo
def function(x):
    return y

cluster analysis

import pandas as pd
import numpy as np 
import  as plt 
import seaborn as sns
import matplotlib as mpl 
from datetime import date, datetime
import streamlit as st  

st.set_page_config(
    page_title="Cohorts Dashboard",
    page_icon="📈",
    layout="wide",
)

@st.experimental_memo
def purchase_rate(customer_id):
    purchase_rate = [1]
    counter = 1
    for i in range(1,len(customer_id)):
        if customer_id[i] != customer_id[i-1]:
            purchase_rate.append(1)
            counter = 1
        else:
            counter += 1
            purchase_rate.append(counter)
    return purchase_rate
@st.experimental_memo
def join_date(date, purchase_rate):
    join_date = list(range(len(date)))
    for i in range(len(purchase_rate)):
        if purchase_rate[i] == 1:
            join_date[i] = date[i]
        else:
            join_date[i] = join_date[i-1]
    return join_date
  
# I didn't define all the functions here because it would extend the length of the article.
# I will provide the full code at the end of the post.

Now let's create the first screen as shown below. This will first allow the user to upload a file and then run it to produce output.

Code template: you can replace it with any name "my_company"。

("Cohort Interactive Dashboard Demo")
("""
This webapp performs cohort analysis of my_company data!
* **Python libraries used:** base64, pandas, streamlit, numpy, matplotlib, seaborn
* **Data source:** [Shopify](https://company_name./admin)
* You need to select the data file first to proceed.
""")
uploaded_file = st.file_uploader("Choose a file") # To upload a file

Once the user has uploaded a file, the next screen will look like this.

To build this, we need to.

  • Choose the type of cluster: this is a single choice. It can be unique in terms of customer retention, or percentage or AOV (average order value).
  • Selection Queue: This is multi-selection. One can look at a specific queue or more.

code template

If uploaded_file is not None:# It's important because there's no such thing. #
                             # When there are no uploaded files, the
                             # An error because df is not defined ....
  df = pd.read_csv(upload_file) # Read the file
  df_processed = process_df(df) # Cleaning data
  
  # Dashboard title
  ("Live Dashboard")
  # Filter
  first_filter = ('Select first filter',['Option 1', 'Option 2', 'Option 3])

    second_filter = ('Select second filter', ['Option 1','Option 2','Option 3','Option 4'])

    output = display_function(data_input,first_filter,second_filter)
    (output)
    st.download_button(label='Download csv', data=output.to_csv(), mime='text/csv') # To download the file

cluster analysis

if uploaded_file is not None:
    df = pd.read_csv(uploaded_file)
    df_processed = process_df(df)
    df_cohorts = cohort_numbers(df_processed)
    cohorts = cohort_percent(df_cohorts)
    
    # Dynamic headings through the use of f-strings
    (f"Live {[0]} to {[-1]} Cohort Dashboard")
    
    # Filter
    first_filter= ('Select type of cohort',['By unique customers', 'By percentage', 'By AOV'])

    second_filter = ('Select cohort', list())

    output = select_which_table_to_draw(df_processed,first_filter,second_filter)
    (output)
    st.download_button(label='Download csv', data=output.to_csv(), mime='text/csv')

Finalization of 3 indicators

code template

kpi1, kpi2, kpi3 = (3) # Create three placeholders
if uploaded_file is not None:
    
    aov = (df['total_sales'])
    aov_goal = 95.00
    (
        # Label this indicator
        label="AOV", 
        # Calculate metrics
        value=f"$ {round(aov,2)}",
        # Calculated change from target (up/down arrows)
        delta=f"-${round(aov_goal-aov,2)}" if aov_goal>aov else f"${round(aov-aov_goal,2)}",
    )

    nc = ([df['customer_type']=='First-time'].groupby(['day']).count()['customer_id'])
    nc_goal = 30
    (
        label="New customers/day",
        value=int(nc),
        delta=f"-{round((nc_goal-nc)/nc_goal*100,2)}%" if nc_goal>nc else f"{round((nc - nc_goal)/nc_goal*100,0)}%",
    )

    rc = ([df['customer_type']=='Returning'].groupby(['day']).count()['customer_id'])
    rc_goal = 250
    (
        label="Returning customers/day",
        value= int(rc),
        delta=f"-{round((rc_goal - rc)/rc_goal*100,2)}%" if rc_goal>rc else f"{round((rc-rc_goal)/rc_goal*100,2)}%"
    )

2. Run the file in a terminal to display it on the local machine

streamlit run 

In the top right corner, you'll see an option to always run whenever you edit.

3. Deploying Dashboards on Heroku

First create a GitHub repository on your GitHub account

establishrespond in singingProcfile

(This is to add all the necessary libraries that you will use in the)

# Simply list all the libraries. You can also include the version
pandas
numpy
streamlit
matplotlib
seaborn
datetime
plotly

#Just copy and paste this
mkdir -p ~/.streamlit/
echo "\
[server]\n\
headless = true\n\
port = $PORT\n
enableCORS = false\n
\n\
" > ~/.streamlit/

Procfile

web: sh  && streamlit run 

  • commander-in-chief (military)Upload to Resource Library
  • Open Heroku (you should create an account on Heroku).

In the upper right corner, click "Create new app"

Under Deployment method, select GitHub

Then connect your GitHub account to Heroku. and enter the name of the repository (in this case, it's cohort_analysis_demo).

Once connected, under Manual Deployment, click Deploy Branches

By this point we just need to wait for it to deploy. Then it's done! The URL link will be created.

Having just deployed the dashboard to production, team members can now access and analyze the data themselves.

They can simply upload any dataset that matches the format you define. I.e. customer transaction data in this case.

final result

summarize

I hope this helps and is only used as a demo reference. Streamlit can actually do a lot of cool things, and if you're like me, you can use it to quickly create a dashboard without knowing web development frameworks like Nodejs, Flask, and Django.

Having said that, I still believe that an understanding of JS/HTML/CSS and software engineering concepts would be an added advantage. So I do recommend that you spend your free time learning these technologies.

to this article on the implementation of Python to build a dashboard sample code is introduced to this article, more related to Python build dashboard content please search my previous posts or continue to browse the following related articles I hope that you will support me in the future more!