DEV Community

masoomjethwa
masoomjethwa

Posted on

Python dashboard

A dashboard attempt

import random
import pandas as pd
import numpy as np
from bokeh.plotting import figure, output_notebook, show
from bokeh.models import ColumnDataSource
from bokeh.layouts import gridplot
from IPython.display import HTML, display

# Generate random data for 10 different plots
data = {
    "scatter": {
        "x": [random.random() for _ in range(100)],
        "y": [random.random() for _ in range(100)],
        "color": [random.choice(["red", "green", "blue"]) for _ in range(100)],
    },
    "line": {
        "x": [i for i in range(100)],
        "y": [random.random() for _ in range(100)],
    },
    "histogram": {
        "values": [random.gauss(0, 1) for _ in range(1000)],
    },
    "pie": {
        "values": [random.randint(1, 10) for _ in range(5)],
        "labels": [f"Label {i+1}" for i in range(5)],
    },
    # Add more types of plots here...
}

# Create DataFrames and ColumnDataSources for each plot
sources = {plot_type: ColumnDataSource(pd.DataFrame(d)) for plot_type, d in data.items()}

# Create a list of figures for each plot
plots = []
for plot_type, source in sources.items():
    p = figure(
        x_axis_label="x",
        y_axis_label="y",
        title=f"{plot_type.capitalize()} Plot",
        width=300,
        height=250,
        toolbar_location=None,  # Hide the default toolbar
    )

    if plot_type == "scatter":
        p.circle(
            x="x",
            y="y",
            color="color",
            legend_field="color",
            size=8,
            source=source,
        )
    elif plot_type == "line":
        p.line(
            x="x",
            y="y",
            source=source,
            line_width=2,
        )
    elif plot_type == "histogram":
        hist, edges = np.histogram(source.data["values"], bins=20)
        p.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], fill_color="blue", line_color="white", alpha=0.7)
    elif plot_type == "pie":
        p.wedge(
            x=0,
            y=0,
            radius=0.4,
            start_angle=0,
            end_angle="values",
            line_color="white",
            source=source,
            legend_field="labels",
        )

    p.title.align = "center"
    p.title.text_font_size = "16pt"
    p.xaxis.axis_label_text_font_size = "12pt"
    p.yaxis.axis_label_text_font_size = "12pt"

    plots.append(p)

# Create a grid layout
layout = gridplot([plots[:3], plots[3:6], plots[6:]])

# Output the plots directly into the Colab notebook
output_notebook()
handle = show(layout, notebook_handle=True)
display(HTML("<p style='text-align: center;'>EDA Dashboard</p>"), display_id="dashboard")
display(handle)
Enter fullscreen mode Exit fullscreen mode

Top comments (0)