I have an issue, I'm after creating a data entry gui to take users input and populate an excel file using pysimplegui, pandas & openpyxl.

I want to display these column values to aid in data entry.

I understand the keys in the script need to match that of the column headers in the excel file but my issue is that 4 of the 6 columns are not to be altered (pulled from database in a separate scheduled script), with only 2 columns needing to get populated with additional information from the user.

How can I get these 4 columns (date, length, calories, duration) to display the information of the next row without information filled in for the other 2 columns (feeling, stress) and then allow for the 2 following keys to get populated?

Code

import pandas as pd

data = {
    "date": ["2023-01-02", "2023-02-01", "2023-04-18"],
    "length": [5, 4.5, 4.2],
    "calories": [420, 380, 390],
    "duration": [50, 40, 45],
    "feeling": ["", "", ""],
    "stress": ["", "", ""]
}

#load data into a DataFrame object:
df = pd.DataFrame(data)

Original Excel File (Sourced form other script querying dbase): Data_Entry.xlsx

date            length      calories        duration    feeling    stress
2023-01-02      5.0         420             50      
2023-02-01      4.5         380             40      
2023-04-18      4.2         390             45      

My main issue is getting the layout section to pull the values for the given keys/columns and allow to populate empty cell values for the remaining 2 columns cells.

import PySimpleGUI as sg

EXCEL_FILE = current_dir / 'Data_Entry.xlsx'

# Load the data if the file exists, if not, create a new DataFrame
if EXCEL_FILE.exists():
    df = pd.read_excel(EXCEL_FILE)
else:
    df = pd.DataFrame()


layout = [
[sg.Text('Date', size=(15,1)), sg.InputText(key='date')],
[sg.Text('Length', size=(15,1)), sg.InputText(key='length')],
[sg.Text('Calories', size=(15,1)), sg.InputText(key='calories')],
[sg.Text('Duration', size=(15,1)), sg.InputText(key='duration')],
sg.Text('Feeling', size=(15,1)), sg.Combo(['Great', 'Okay', 'Bad'], key='feeling')],
sg.Text('Stress', size=(15,1)), sg.Combo(['Mild', 'Slight', 'Extreme'], key='stress')],
[sg.Submit(), sg.Button("Clear"), sg.Exit()]
]

window = sg.Window("Record Welness", layout)


def clear_input():
    for key in values:
        window[key]("")
    return None

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event == 'Clear':
        clear_input()
    if event == 'Submit':
        new_record = pd.DataFrame(values, index=[0])
        df = pd.concat([df, new_record], ignore_index=True)
        df.to_excel(EXCEL_FILE, index=False)  # This will create the file if it doesn't exist
        sg.popup('Data saved!')
        clear_input()
window.close()
0

There are 0 best solutions below