Ethereal Data Visualizer Python GUI

👤 Sharing: AI
import tkinter as tk
from tkinter import ttk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import pandas as pd
import numpy as np
import random

class DataVisualizerApp:
    def __init__(self, master):
        self.master = master
        master.title("Ethereal Data Visualizer")

        self.notebook = ttk.Notebook(master)
        self.notebook.pack(pady=10, padx=10, expand=True, fill="both")

        self.tab_random = ttk.Frame(self.notebook)
        self.tab_csv = ttk.Frame(self.notebook)

        self.notebook.add(self.tab_random, text="Random Data")
        self.notebook.add(self.tab_csv, text="CSV Data")

        self.create_random_tab()
        self.create_csv_tab()

        self.details_button = tk.Button(master, text="Details", command=self.show_details)
        self.details_button.pack(pady=5)

    def create_random_tab(self):
        # Random Data Generation
        self.num_points = tk.IntVar(value=50)
        self.num_series = tk.IntVar(value=3)

        tk.Label(self.tab_random, text="Number of Points:").grid(row=0, column=0, padx=5, pady=5)
        tk.Entry(self.tab_random, textvariable=self.num_points).grid(row=0, column=1, padx=5, pady=5)

        tk.Label(self.tab_random, text="Number of Series:").grid(row=1, column=0, padx=5, pady=5)
        tk.Entry(self.tab_random, textvariable=self.num_series).grid(row=1, column=1, padx=5, pady=5)

        self.random_plot_button = tk.Button(self.tab_random, text="Generate Random Plot", command=self.generate_random_plot)
        self.random_plot_button.grid(row=2, column=0, columnspan=2, pady=10)

        self.random_figure, self.random_ax = plt.subplots(figsize=(6, 4), dpi=100)
        self.random_canvas = FigureCanvasTkAgg(self.random_figure, master=self.tab_random)
        self.random_canvas.get_tk_widget().grid(row=3, column=0, columnspan=2, padx=10, pady=10)
        self.random_canvas.draw()

    def create_csv_tab(self):
        # CSV Data Input
        self.csv_path = tk.StringVar()

        tk.Label(self.tab_csv, text="CSV File Path:").grid(row=0, column=0, padx=5, pady=5)
        tk.Entry(self.tab_csv, textvariable=self.csv_path, width=40).grid(row=0, column=1, padx=5, pady=5)

        self.csv_plot_button = tk.Button(self.tab_csv, text="Generate CSV Plot", command=self.generate_csv_plot)
        self.csv_plot_button.grid(row=1, column=0, columnspan=2, pady=10)

        self.csv_figure, self.csv_ax = plt.subplots(figsize=(6, 4), dpi=100)
        self.csv_canvas = FigureCanvasTkAgg(self.csv_figure, master=self.tab_csv)
        self.csv_canvas.get_tk_widget().grid(row=2, column=0, columnspan=2, padx=10, pady=10)
        self.csv_canvas.draw()

    def generate_random_plot(self):
        self.random_ax.clear()
        num_points = self.num_points.get()
        num_series = self.num_series.get()

        for _ in range(num_series):
            x = np.arange(num_points)
            y = np.random.rand(num_points)
            self.random_ax.plot(x, y)

        self.random_ax.set_xlabel("X-axis")
        self.random_ax.set_ylabel("Y-axis")
        self.random_ax.set_title("Random Data Plot")
        self.random_ax.grid(True)
        self.random_canvas.draw()

    def generate_csv_plot(self):
        self.csv_ax.clear()
        try:
            df = pd.read_csv(self.csv_path.get())
            # Assuming first two columns are x and y
            x = df.iloc[:, 0]
            y = df.iloc[:, 1]
            self.csv_ax.plot(x, y)

            self.csv_ax.set_xlabel(df.columns[0])
            self.csv_ax.set_ylabel(df.columns[1])
            self.csv_ax.set_title("CSV Data Plot")
            self.csv_ax.grid(True)
            self.csv_canvas.draw()
        except FileNotFoundError:
            self.show_error("File Not Found", "The specified CSV file was not found.")
        except Exception as e:
            self.show_error("Error", str(e))

    def show_error(self, title, message):
        tk.messagebox.showerror(title, message)

    def show_details(self):
        details_window = tk.Toplevel(self.master)
        details_window.title("Program Details")
        details_text = tk.Text(details_window, wrap=tk.WORD, width=60, height=20)
        details_text.pack(padx=10, pady=10)
        details_text.insert(tk.END, """
        Ethereal Data Visualizer is a versatile GUI application built with Python and Tkinter, designed to visualize data from two different sources:

        1.  **Random Data Generation Tab:** This tab allows users to generate random data and visualize it as a plot. Users can specify the number of data points and the number of series to plot.  This is useful for testing and demonstration purposes.

        2.  **CSV Data Visualization Tab:** This tab allows users to input the file path of a CSV file and visualize the data within it.  The program assumes that the first two columns of the CSV file represent the x and y values for the plot. Error handling is included to manage file not found errors and other potential issues during CSV parsing.

        The application uses Matplotlib for plotting and Pandas for reading CSV files.  It is structured to be modular and easily extensible with additional visualization options or data sources.
        """)
        details_text.config(state=tk.DISABLED) # Make the text read-only


root = tk.Tk()
app = DataVisualizerApp(root)
root.mainloop()
👁️ Viewed: 6

Comments