Machine Learning

Creating a modern dashboard with Python and Gradio

Secondly in a short series of data Dashboard development tools for Vython Development tools that are based on the power, distribution, gridio, and Tapy.

Each Dashboard's source data will be similar, but stored in different formats. As much as possible, I will try and make real Dashboard structures to be similar and effective.

In the first part of this series, I created a Dashboard Discibt of taking its data from the Postgreesql area database. You can look at that article here.

Currently, we test the use of gradio Library.

The details of the dashboard will be in the local CSV file, while the panda will be our main engine for data processing.

If you want to see an app immediately for the app, I sent to arrest the faces of the face. You can run the link below, but notice that POPs – the two installation cookies do not work because of the known bug in the distance of the distance. This is the end of the apps sent to HF, you can still change days manually. Running the app in your area is effective and no matter.

Demo Demobboard to Huggingface

What is Gradio?

Gradio is an open package of Python Lisa process to create a construction process or programming apps for the machine reading models, APIs, any Python function. For it, you can create Demos or Web apps or applications without needing JavaScript, CSS, or the Web hosting experience. By writing a few lines of the Python code, you can unlock the Gradio power and display without seamless mask models in a broader audience.

Gradio facilitates the development process by providing accurate framework that removes difficulties associated with the construction of the user's construction sites from the beginning. Whether you are an enhancement of the machine, researcher, or enthusiasm, the Gradio allows you to create positive and effective demos that improve the understanding and accessibility of your machine reading models.

This open Python packet helps you to block the gap between your machine reading technology and broader audience, making your models accessible and activate.

What to Improve

We are upgrading the Data Dashboard. Our source data will be one CSV file that contains 100,000 sales records.

The real source of data is not that It's important. It is just possible that it is a text file easily, the Excel file, SQLITE, any type of data you can connect to it.

This is what our last dashboard will look.

Photo by the writer

There are four main categories.

  • The top line allows the user to select the first time and endings and / or product stages using time-dropped lists, respectively.
  • Second line – Key Meyri – showing a high-level summary of the selected data.
  • The sequence class allows the user to choose one of the three graphs to show the installation data.
  • The raw data part is exactly what it will be. This type of selected data table shows successfully a summary of the CSV file summary.

Using the dashboard is easy. Initially, the whole data set statistics are displayed. The user may then reduce the focus of data using three files of sorting over the display. Graphs, key metrics, and parts of renewable green data to update to display user decisions in filters.

Basic data

As mentioned, Dashboard source data contained in one commander of the Comma-separated by the comma (CSV). Details contains records related to sales relating to the performance. Here are ten first file records to give you the view of what it look like.

+----------+------------+------------+----------------+------------+---------------+------------+----------+-------+--------------------+
| order_id | order_date | customer_id| customer_name  | product_id | product_names | categories | quantity | price | total              |
+----------+------------+------------+----------------+------------+---------------+------------+----------+-------+--------------------+
| 0        | 01/08/2022 | 245        | Customer_884   | 201        | Smartphone    | Electronics| 3        | 90.02 | 270.06             |
| 1        | 19/02/2022 | 701        | Customer_1672  | 205        | Printer       | Electronics| 6        | 12.74 | 76.44              |
| 2        | 01/01/2017 | 184        | Customer_21720 | 208        | Notebook      | Stationery | 8        | 48.35 | 386.8              |
| 3        | 09/03/2013 | 275        | Customer_23770 | 200        | Laptop        | Electronics| 3        | 74.85 | 224.55             |
| 4        | 23/04/2022 | 960        | Customer_23790 | 210        | Cabinet       | Office     | 6        | 53.77 | 322.62             |
| 5        | 10/07/2019 | 197        | Customer_25587 | 202        | Desk          | Office     | 3        | 47.17 | 141.51             |
| 6        | 12/11/2014 | 510        | Customer_6912  | 204        | Monitor       | Electronics| 5        | 22.5  | 112.5              |
| 7        | 12/07/2016 | 150        | Customer_17761 | 200        | Laptop        | Electronics| 9        | 49.33 | 443.97             |
| 8        | 12/11/2016 | 997        | Customer_23801 | 209        | Coffee Maker  | Electronics| 7        | 47.22 | 330.54             |
| 9        | 23/01/2017 | 151        | Customer_30325 | 207        | Pen           | Stationery | 6        | 3.5   | 21                 |
+----------+------------+------------+----------------+------------+---------------+------------+----------+-------+--------------------+

And here is another Python code that you can use to produce the same data. Make sure the Nunpy libraries and PANDAS is first installed.

# generate the 100K record CSV file
#
import polars as pl
import numpy as np
from datetime import datetime, timedelta

def generate(nrows: int, filename: str):
    names = np.asarray(
        [
            "Laptop",
            "Smartphone",
            "Desk",
            "Chair",
            "Monitor",
            "Printer",
            "Paper",
            "Pen",
            "Notebook",
            "Coffee Maker",
            "Cabinet",
            "Plastic Cups",
        ]
    )
    categories = np.asarray(
        [
            "Electronics",
            "Electronics",
            "Office",
            "Office",
            "Electronics",
            "Electronics",
            "Stationery",
            "Stationery",
            "Stationery",
            "Electronics",
            "Office",
            "Sundry",
        ]
    )
    product_id = np.random.randint(len(names), size=nrows)
    quantity = np.random.randint(1, 11, size=nrows)
    price = np.random.randint(199, 10000, size=nrows) / 100
    # Generate random dates between 2010-01-01 and 2023-12-31
    start_date = datetime(2010, 1, 1)
    end_date = datetime(2023, 12, 31)
    date_range = (end_date - start_date).days
    # Create random dates as np.array and convert to string format
    order_dates = np.array([(start_date + timedelta(days=np.random.randint(0, date_range))).strftime('%Y-%m-%d') for _ in range(nrows)])
    # Define columns
    columns = {
        "order_id": np.arange(nrows),
        "order_date": order_dates,
        "customer_id": np.random.randint(100, 1000, size=nrows),
        "customer_name": [f"Customer_{i}" for i in np.random.randint(2**15, size=nrows)],
        "product_id": product_id + 200,
        "product_names": names[product_id],
        "categories": categories[product_id],
        "quantity": quantity,
        "price": price,
        "total": price * quantity,
    }
    # Create Polars DataFrame and write to CSV with explicit delimiter
    df = pl.DataFrame(columns)
    df.write_csv(filename, separator=',',include_header=True)  # Ensure comma is used as the delimiter

# Generate 100,000 rows of data with random order_date and save to CSV
generate(100_000, "/mnt/d/sales_data/sales_data.csv")

To add and use gradio

To Install Gradio Easy to Use pateBut in the codes, the best practice is to set a separate Python area for all your work. I am using the goal and for that purpose, but feel free to use any method that suits your work work.

If you want to decrease the collar route and you don't have, you must include a minicondonda (recommended) or anacondo first.

Please note that, at the time of writing, Gradio requires at least the Python 3.8 incorporated performance.

Where nature is created, switch to it using 'Active' order, and run 'PIP Install' above Apply our required Python libraries.

#create our test environment
(base) C:Usersthoma>conda create -n gradio_dashboard python=3.12 -y

# Now activate it
(base) C:Usersthoma>conda activate gradio_dashboard

# Install python libraries, etc ...
(gradio_dashboard) C:Usersthoma>pip install gradio pandas matplotlib cachetools

The Important Differences Between Brado and Gradio

As I will show in this article, it is possible to generate the same data dashboards using distribution and gradio. However, their ethos varies in a few important ways.

Focus on understanding

  • The Gradio works specifically to create a machine reading system systems, while the distribution is more for normal data applications and flatting.

Easy Use

  • Gradio is known as its simplicity and speedy skills of Prototyping, making it easier for beginners to use. Streamlit offers advanced features and customization options, which may require strong learning curve.

Imposing

  • Streamlit uses a functional planning model where any installation change causes the perfect run recin, renew all things immediately. Gradio, automatically, only updates when user clicks on the navigation button, or can be adjusted for live updates.

Customize

  • Gradio focuses on the previously built-in components to promote AI models. Streamlit provides many additional options for customization and fluctuations of complex projects.

Submission

  • As we have sent both the broadcast app and the Gradio app, I was easy to install the streaming app is better than the Gradio app. In the streamlit, shipment can be made by one click with streamlit Community Cloud. This function is built on any application of the Saldriver app. The Gradio provides submission is used to arrest facial spaces, but including additional function. Even if it is particularly complicated.

Use charges

The symstlit is the best in creating Centruc apps and operating data and dashboards are complicated projects. Gradio is ready for machine learning models immediately and to create apps.

Gradio Dashboard Code

I will decrease the code into categories and explain each one as we continue.

We start by adding the external libraries needed and loaded the perfect dataset from the CSV file into Pandas data.

import gradio as gr
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import warnings
import os
import tempfile
from cachetools import cached, TTLCache

warnings.filterwarnings("ignore", category=FutureWarning, module="seaborn")

# ------------------------------------------------------------------
# 1) Load CSV data once
# ------------------------------------------------------------------
csv_data = None

def load_csv_data():
    global csv_data
    
    # Optional: specify column dtypes if known; adjust as necessary
    dtype_dict = {
        "order_id": "Int64",
        "customer_id": "Int64",
        "product_id": "Int64",
        "quantity": "Int64",
        "price": "float",
        "total": "float",
        "customer_name": "string",
        "product_names": "string",
        "categories": "string"
    }
    
    csv_data = pd.read_csv(
        "d:/sales_data/sales_data.csv",
        parse_dates=["order_date"],
        dayfirst=True,      # if your dates are DD/MM/YYYY format
        low_memory=False,
        dtype=dtype_dict
    )

load_csv_data()

Next, preparing for a period of time to go to the Live-Live with 128 thousand and running out of 300 seconds. This is used to store the effects of expensive work call results and speed up repeated observation

This page Get_unique_catories The employee restores the different, cleaned (conducted) categories) from the DataFrame `CSV_Data`, Fasty-Refering Fasting Defense.

This page Get_date_Range The work returns the lower days and high dataset dataset, or no data is not available.

This page filter_data Sorting of work CSV_Data DataFrame based on the specified Detected Date and optional category, returning the Dataframrame.

This page Get_dashboard_stats Metric Metric Metrics – Current Money, Perfect Orders, Maximum Order Ordo, and Top Division – Features provided. Inside uses filter_data() To put the scope the dataset and count these important figures.

This page Get_data_for_table fUnion returns a detailed Dataafammame for the filtered data, scheduled Order_id including Order_dateincluding additional sales cash.

This page Get_plot_data The work format of a conspiracy format by clouding money over time, organized in a collection.

This page Get_revenue_by_category Integrated work and restore income in section, organized, within a specified Dutch grade and category.

This page Get_top_products The work returns 10 high products for income, filtered by Rede Range and Category.

Based on Oriental conflict, the creating_matplotlib_figire The work produces bar plot from detail and saves as a picture file, either vertical or horizontal.

cache = TTLCache(maxsize=128, ttl=300)

@cached(cache)
def get_unique_categories():
    global csv_data
    if csv_data is None:
        return []
    cats = sorted(csv_data['categories'].dropna().unique().tolist())
    cats = [cat.capitalize() for cat in cats]
    return cats

def get_date_range():
    global csv_data
    if csv_data is None or csv_data.empty:
        return None, None
    return csv_data['order_date'].min(), csv_data['order_date'].max()

def filter_data(start_date, end_date, category):
    global csv_data

    if isinstance(start_date, str):
        start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d').date()
    if isinstance(end_date, str):
        end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d').date()

    df = csv_data.loc[
        (csv_data['order_date'] >= pd.to_datetime(start_date)) &
        (csv_data['order_date'] <= pd.to_datetime(end_date))
    ].copy()

    if category != "All Categories":
        df = df.loc[df['categories'].str.capitalize() == category].copy()

    return df

def get_dashboard_stats(start_date, end_date, category):
    df = filter_data(start_date, end_date, category)
    if df.empty:
        return (0, 0, 0, "N/A")

    df['revenue'] = df['price'] * df['quantity']
    total_revenue = df['revenue'].sum()
    total_orders = df['order_id'].nunique()
    avg_order_value = total_revenue / total_orders if total_orders else 0

    cat_revenues = df.groupby('categories')['revenue'].sum().sort_values(ascending=False)
    top_category = cat_revenues.index[0] if not cat_revenues.empty else "N/A"

    return (total_revenue, total_orders, avg_order_value, top_category.capitalize())

def get_data_for_table(start_date, end_date, category):
    df = filter_data(start_date, end_date, category)
    if df.empty:
        return pd.DataFrame()

    df = df.sort_values(by=["order_id", "order_date"], ascending=[True, False]).copy()

    columns_order = [
        "order_id", "order_date", "customer_id", "customer_name",
        "product_id", "product_names", "categories", "quantity",
        "price", "total"
    ]
    columns_order = [col for col in columns_order if col in df.columns]
    df = df[columns_order].copy()

    df['revenue'] = df['price'] * df['quantity']
    return df

def get_plot_data(start_date, end_date, category):
    df = filter_data(start_date, end_date, category)
    if df.empty:
        return pd.DataFrame()
    df['revenue'] = df['price'] * df['quantity']
    plot_data = df.groupby(df['order_date'].dt.date)['revenue'].sum().reset_index()
    plot_data.rename(columns={'order_date': 'date'}, inplace=True)
    return plot_data

def get_revenue_by_category(start_date, end_date, category):
    df = filter_data(start_date, end_date, category)
    if df.empty:
        return pd.DataFrame()
    df['revenue'] = df['price'] * df['quantity']
    cat_data = df.groupby('categories')['revenue'].sum().reset_index()
    cat_data = cat_data.sort_values(by='revenue', ascending=False)
    return cat_data

def get_top_products(start_date, end_date, category):
    df = filter_data(start_date, end_date, category)
    if df.empty:
        return pd.DataFrame()
    df['revenue'] = df['price'] * df['quantity']
    prod_data = df.groupby('product_names')['revenue'].sum().reset_index()
    prod_data = prod_data.sort_values(by='revenue', ascending=False).head(10)
    return prod_data

def create_matplotlib_figure(data, x_col, y_col, title, xlabel, ylabel, orientation='v'):
    plt.figure(figsize=(10, 6))
    if data.empty:
        plt.text(0.5, 0.5, 'No data available', ha='center', va='center')
    else:
        if orientation == 'v':
            plt.bar(data[x_col], data[y_col])
            plt.xticks(rotation=45, ha='right')
        else:
            plt.barh(data[x_col], data[y_col])
            plt.gca().invert_yaxis() 

    plt.title(title)
    plt.xlabel(xlabel)
    plt.ylabel(ylabel)
    plt.tight_layout()

    with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmpfile:
        plt.savefig(tmpfile.name)
    plt.close()
    return tmpfile.name

This page Update_Dashboard Work to return the sales key (total money, full orders, middle order value, and maximum category) by callingget_dashboard_stats work. Gathering three different living organism data (income later, income in section, and higher products), then use create_matplotlib_figure to produce the sites. Prepares and returns data table (with get_data_for_table() An employee) and all the sites produced and statistics to be shown in the dashboard.

This page Create_dashboard The work sets the days of the day (minimum and upper days) and establishes automatic filters automatic. Using Gradio To create a user interface (UI) that shows the timeshellers, drop-down section, to show the metric, plot tabs, and data table. Opens filters to change any of them to cause the call to Update_Dashboard The work, to ensure dashboards and metric observations always synchronizing the selected filters. Finally, returns a collection of a collection of Gradio presented as a web app.

def update_dashboard(start_date, end_date, category):
    total_revenue, total_orders, avg_order_value, top_category = get_dashboard_stats(start_date, end_date, category)

    # Generate plots
    revenue_data = get_plot_data(start_date, end_date, category)
    category_data = get_revenue_by_category(start_date, end_date, category)
    top_products_data = get_top_products(start_date, end_date, category)

    revenue_over_time_path = create_matplotlib_figure(
        revenue_data, 'date', 'revenue',
        "Revenue Over Time", "Date", "Revenue"
    )
    revenue_by_category_path = create_matplotlib_figure(
        category_data, 'categories', 'revenue',
        "Revenue by Category", "Category", "Revenue"
    )
    top_products_path = create_matplotlib_figure(
        top_products_data, 'product_names', 'revenue',
        "Top Products", "Revenue", "Product Name", orientation='h'
    )

    # Data table
    table_data = get_data_for_table(start_date, end_date, category)

    return (
        revenue_over_time_path,
        revenue_by_category_path,
        top_products_path,
        table_data,
        total_revenue,
        total_orders,
        avg_order_value,
        top_category
    )

def create_dashboard():
    min_date, max_date = get_date_range()
    if min_date is None or max_date is None:
        min_date = datetime.datetime.now()
        max_date = datetime.datetime.now()

    default_start_date = min_date
    default_end_date = max_date

    with gr.Blocks(css="""
        footer {display: none !important;}
        .tabs {border: none !important;}  
        .gr-plot {border: none !important; box-shadow: none !important;}
    """) as dashboard:
        
        gr.Markdown("# Sales Performance Dashboard")

        # Filters row
        with gr.Row():
            start_date = gr.DateTime(
                label="Start Date",
                value=default_start_date.strftime('%Y-%m-%d'),
                include_time=False,
                type="datetime"
            )
            end_date = gr.DateTime(
                label="End Date",
                value=default_end_date.strftime('%Y-%m-%d'),
                include_time=False,
                type="datetime"
            )
            category_filter = gr.Dropdown(
                choices=["All Categories"] + get_unique_categories(),
                label="Category",
                value="All Categories"
            )

        gr.Markdown("# Key Metrics")

        # Stats row
        with gr.Row():
            total_revenue = gr.Number(label="Total Revenue", value=0)
            total_orders = gr.Number(label="Total Orders", value=0)
            avg_order_value = gr.Number(label="Average Order Value", value=0)
            top_category = gr.Textbox(label="Top Category", value="N/A")

        gr.Markdown("# Visualisations")
        # Tabs for Plots
        with gr.Tabs():
            with gr.Tab("Revenue Over Time"):
                revenue_over_time_image = gr.Image(label="Revenue Over Time", container=False)
            with gr.Tab("Revenue by Category"):
                revenue_by_category_image = gr.Image(label="Revenue by Category", container=False)
            with gr.Tab("Top Products"):
                top_products_image = gr.Image(label="Top Products", container=False)

        gr.Markdown("# Raw Data")
        # Data Table (below the plots)
        data_table = gr.DataFrame(
            label="Sales Data",
            type="pandas",
            interactive=False
        )

        # When filters change, update everything
        for f in [start_date, end_date, category_filter]:
            f.change(
                fn=lambda s, e, c: update_dashboard(s, e, c),
                inputs=[start_date, end_date, category_filter],
                outputs=[
                    revenue_over_time_image, 
                    revenue_by_category_image, 
                    top_products_image,
                    data_table,
                    total_revenue, 
                    total_orders,
                    avg_order_value, 
                    top_category
                ]
            )

        # Initial load
        dashboard.load(
            fn=lambda: update_dashboard(default_start_date, default_end_date, "All Categories"),
            outputs=[
                revenue_over_time_image, 
                revenue_by_category_image, 
                top_products_image,
                data_table,
                total_revenue, 
                total_orders,
                avg_order_value, 
                top_category
            ]
        )

    return dashboard

if __name__ == "__main__":
    dashboard = create_dashboard()
    dashboard.launch(share=False)

To run the system

Create a Python file, e.g. Gradio_Test.py, and enter all the snacks of the code above. Keep it, and run it fast like this,

(gradio_dashboard) $ python gradio_test.py

* Running on local URL:  

To create a public link, set `share=True` in `launch()`.

Click on the indicated location URL, and the Dashboard will open full screen in your browser.

Summary

This document provides the full guideline for the Interactive Performance Performance Dashboard using Gradio and CSV file as its source data.

The Gradio is a framework of the open source based on Python that facilitates the creation of dashboards and the GUI apps. I'm upgraded to users to sort out data for long distances and products, evaluate higher metrics such as top lessons and high products, and roam with green data via PageNage.

I also talked about important differences between developing eye-watch tools using Gradio and Streamlit, another famous Python library.

This guide provides the complete use of Gradio Disciplio, covering the all process from creating a sample data in creating Python's inquiry data, resolving the user's installation. This approach of step-by-step shows how you can be the strength of the Grid Gridio creating more vulnerable and dynamic devices, which makes it eligible for data and scientists who want to create effective data agents.

Although I have used the CSV file of my details, I adjust the code to use another data source, such as Database Management as SQLITE, you must understand. For example, one of my article in this series by building the same dashboard using streamlit, a data source is postgresql data.

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button