Skip to content

Commit

Permalink
added name to CSV import
Browse files Browse the repository at this point in the history
  • Loading branch information
edouardbruelhart committed Apr 15, 2024
1 parent bc7c0fe commit b7ca220
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions ms_sample_list_creator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
# You can generate windows executable from linux using wine, by previously installing wine, python 3.8.19, pyinstaller and
# other non-built-in packages (here requests and pandas) inside wine. Then run: wine pyinstaller --onefile ms_sample_list_creator.py

import csv
import os
import tkinter as tk
from datetime import datetime
from tkinter import filedialog, ttk
from typing import Any, Optional
from datetime import datetime
import csv

import pandas as pd
import requests
Expand Down Expand Up @@ -197,7 +197,9 @@ def __init__(self, parent: tk.Tk, *args: Any, **kwargs: Any):
frame_submit = tk.Frame(self)
frame_submit.pack(pady=(50, 0))

button_new_batch = tk.Button(frame_submit, text="New sample list", width=20, command=lambda: self.show_values("new"))
button_new_batch = tk.Button(
frame_submit, text="New sample list", width=20, command=lambda: self.show_values("new")
)
button_new_batch.pack(side="left")

button_submit_csv = tk.Button(
Expand Down Expand Up @@ -424,6 +426,7 @@ def manage_choice(self) -> None:
# If user didn't enter all necessary values, shows this message
self.label.config(text="Unknow error, please try again with other parameters", foreground="red")


class newBatch:
def __init__(self, new_batch_window: tk.Toplevel, root: tk.Tk):
"""
Expand Down Expand Up @@ -809,6 +812,7 @@ def store_prefix(self) -> None:
# Close the AskBoxPrefixWindow
self.master.destroy()


class csvBatch(tk.Frame):
def __init__(self, csv_batch_window: tk.Toplevel, root: tk.Tk):
"""
Expand Down Expand Up @@ -846,7 +850,7 @@ def __init__(self, csv_batch_window: tk.Toplevel, root: tk.Tk):
self.file = str(os.environ.get("FILE"))
self.current_position = 1
self.current_row = 1
self.timestamp = "202404101527" # datetime.now().strftime("%Y%m%d%H%M")
self.timestamp = datetime.now().strftime("%Y%m%d%H%M")
self.csv_path = f"{self.output_folder}/{datetime.now().strftime('%Y%m%d')}_{self.operator}_dbgi_{self.file}.csv"

self.warning_label = tk.Label(
Expand All @@ -858,10 +862,10 @@ def __init__(self, csv_batch_window: tk.Toplevel, root: tk.Tk):
label = tk.Label(self.csv_batch_window, text="Search for your CSV:", pady=10)
label.pack()

import_button = tk.Button(
self.import_button = tk.Button(
self.csv_batch_window, text="Import your CSV", width=17, command=self.import_csv, pady=10
)
import_button.pack()
self.import_button.pack()

button_submit = tk.Button(self.csv_batch_window, text="Submit", width=17, command=self.submit_result, pady=10)
button_submit.pack()
Expand Down Expand Up @@ -892,7 +896,11 @@ def import_csv(self) -> None:
Returns:
None
"""
os.environ["FILE_PATH"] = filedialog.askopenfilename(filetypes=[("CSV Files", "*.csv")])
csv_file = os.environ["FILE_PATH"] = filedialog.askopenfilename(filetypes=[("CSV Files", "*.csv")])
if csv_file:
parts = csv_file.split("/")
file = parts[-1]
self.import_button.config(text=file)

def submit_result(self) -> None:
"""
Expand All @@ -912,7 +920,7 @@ def submit_result(self) -> None:
df = pd.read_csv(str(file_path), skiprows=1)

# Keep only the necessary columns in order to not generate a corrupted CSV
columns_filter = ['File Name', 'Path', 'Instrument Method', 'Position', 'Inj Vol']
columns_filter = ["File Name", "Path", "Instrument Method", "Position", "Inj Vol"]
df = df.loc[:, columns_filter]

# Delete standby row
Expand Down Expand Up @@ -959,7 +967,7 @@ def submit_result(self) -> None:
response = session.post(url=collection_url, headers=headers, data=records)

# Check if correctly added to directus
if response.status_code != 200:
if response.status_code == 200:
self.warning_label.config(text="Success!! Writing CSV...", foreground="green")
# Write data to the CSV file
with open(self.csv_path, "w", newline="") as csv_file:
Expand Down Expand Up @@ -1023,6 +1031,7 @@ def submit_result(self) -> None:
else:
self.warning_label.config(text="Directus error, please check your CSV.", foreground="red")


# Create an instance of the main window
root = tk.Tk()
root.title("Home")
Expand All @@ -1035,4 +1044,4 @@ def submit_result(self) -> None:
home.pack()

# Start the tkinter event loop
root.mainloop()
root.mainloop()

0 comments on commit b7ca220

Please sign in to comment.