SelwynChan / HelloWorld

My first Git Repository
0 stars 0 forks source link

Pm3 #3

Open SelwynChan opened 1 year ago

SelwynChan commented 1 year ago

Here's the entire updated code with the date range input and dummy functions to execute the steps:

import os
import uuid
import json
import tkinter as tk
from tkinter import Frame, Button, Listbox, Tk, filedialog, messagebox, simpledialog, Toplevel, Label, BOTH, DISABLED, NORMAL
import webbrowser
from datetime import datetime
from tkcalendar import DateEntry

class Pipeline:
    def __init__(self, workspace, pipeline_id=None, date_range=None):
        self.id = pipeline_id or str(uuid.uuid4())
        self.workspace = os.path.join(workspace, self.id)
        os.makedirs(self.workspace, exist_ok=True)
        self.status = 'Not started'
        self.date_range = date_range

    def save_pipeline(self):
        with open(os.path.join(self.workspace, 'pipeline.json'), 'w') as f:
            json.dump({'id': self.id, 'workspace': self.workspace, 'status': self.status, 'date_range': self.date_range}, f)

    def download_data(self):
        print(f"Downloading data for pipeline {self.id} from {self.date_range['start_date']} to {self.date_range['end_date']}")

    def process_data(self):
        print(f"Processing data for pipeline {self.id}")

    def generate_report(self):
        print(f"Generating report for pipeline {self.id}")

    def package_data(self):
        print(f"Packaging data for pipeline {self.id}")

class OutputDirectoriesWindow(Toplevel):
    def __init__(self, master, pipeline):
        super().__init__(master)
        self.title("Output Directories")
        self.pipeline = pipeline
        self.init_ui()

    def init_ui(self):
        directory_names = ['raw_data', 'processed_data', 'associated_data', 'combined_data', 'package']
        for name in directory_names:
            directory_path = os.path.join(self.pipeline.workspace, name)
            label = Label(self, text=directory_path, fg="blue", cursor="hand2")
            label.pack(padx=(10, 10), pady=(5, 5))
            label.bind("<Button-1>", lambda event, path=directory_path: self.open_directory(path))

    def open_directory(self, path):
        webbrowser.open(f'file://{path}')

class PipelineManager(Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.workspace = None
        self.pipelines = []
        self.init_ui()

    def init_ui(self):
        self.set_workspace_button = Button(self, text="Set Workspace", command=self.set_workspace)
        self.set_workspace_button.pack(pady=(10, 10))

        self.create_pipeline_button = Button(self, text="Create Pipeline", command=self.create_pipeline, state=DISABLED)
        self.create_pipeline_button.pack(pady=(10, 10))

        self.pipeline_listbox = Listbox(self)
        self.pipeline_listbox.pack(fill=BOTH, expand=True, padx=(10, 10), pady=(10, 10))

        self.start_date_label = Label(self, text="Start Date:")
        self.start_date_label.pack(pady=(10, 5))

        self.start_date_entry = DateEntry(self)
        self.start_date_entry.pack(pady=(0, 10))

        self.end_date_label = Label(self, text="End Date:")
        self.end_date_label.pack(pady=(0, 5))

        self.end_date_entry = DateEntry(self)
        self.end_date_entry.pack(pady=(0, 10))

        self.execute_pipeline_button = Button(self, text="Execute Pipeline", command=self.execute_pipeline)
        self.execute_pipeline_button.pack(pady=(10, 10))

        self.show_outputs_button = Button(self, text="Show Output Directories", command=self.show_output_directories)
        self.show_outputs_button.pack(pady=(10, 10))

    def set_workspace(self):
        workspace = filedialog.askdirectory(title='Select Workspace')
        if workspace:
            self.workspace = workspace
            self.create_pipeline_button.config(state=NORMAL)

    def create_pipeline(self):
        if not self.workspace:
            messagebox.showwarning("Warning", "Workspace not set")
            return

        pipeline_id = simpledialog.askstring("Pipeline ID", "Enter a human-readable ID for the pipeline:")
        if not pipeline_id:
            return

        start_date = self.start_date_entry.get_date()
        end_date = self.end_date_entry.get_date()

        if start_date > end_date:
            messagebox.showwarning("Warning", "Start date cannot be greater than end date")
            return

        date_range = {
            'start_date': start_date.strftime('%Y-%m-%d'),
            'end_date': end_date.strftime('%Y-%m-%d')
        }

        pipeline = Pipeline(self.workspace, pipeline_id, date_range)
        pipeline.save_pipeline()
        self.pipelines.append(pipeline)
        self.add_pipeline_to_listbox(pipeline)

    def add_pipeline_to_listbox(self, pipeline):
        self.pipeline_listbox.insert(tk.END, pipeline.id)

    def execute_pipeline(self):
        if not self.workspace:
            messagebox.showwarning("Warning", "Workspace not set")
            return

        selected_index = self.pipeline_listbox.curselection()
        if not selected_index:
            messagebox.showwarning("Warning", "No pipeline selected")
            return

        selected_pipeline = self.pipelines[selected_index[0]]
        selected_pipeline.download_data()
        selected_pipeline.process_data()
        selected_pipeline.generate_report()
        selected_pipeline.package_data()

    def show_output_directories(self):
        if not self.workspace:
            messagebox.showwarning("Warning", "Workspace not set")
            return

        selected_index = self.pipeline_listbox.curselection()
        if not selected_index:
            messagebox.showwarning("Warning", "No pipeline selected")
            return

        selected_pipeline = self.pipelines[selected_index[0]]
        OutputDirectoriesWindow(self.master, selected_pipeline)

if __name__ == '__main__':
    root = Tk()
    root.geometry("800x600")
    app = PipelineManager(master=root)
    app.mainloop()

In this code, I included the download_data, process_data, generate_report, and package_data dummy functions in the Pipeline class. These functions are called when the "Execute Pipeline" button is clicked in the PipelineManager class.