File size: 4,192 Bytes
7caacb6
 
 
564b785
 
7caacb6
 
 
 
 
 
 
564b785
7caacb6
 
564b785
 
 
 
 
 
7caacb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
564b785
7caacb6
 
564b785
7caacb6
 
 
 
 
 
 
 
564b785
 
7caacb6
564b785
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7caacb6
564b785
 
24be6ef
7caacb6
564b785
 
 
 
 
 
 
 
 
 
 
 
7caacb6
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import streamlit as st
import pandas as pd
from datetime import datetime
import glob
import os

# Function to check user credentials
def check_credentials(username, password):
    return username == "Josia" and password == "admin"

# Function to save data to CSV
def save_data(form_data, username):
    # Convert to DataFrame and append to CSV. Here we assume that all CSV files are stored in the current directory.
    df = pd.DataFrame([form_data])
    csv_path = f'collected_data_{username}.csv'
    df.to_csv(csv_path, mode='a', header=not os.path.exists(csv_path), index=False)

# Function to list all CSV files created by the users
def list_csv_files():
    # List all CSV files in the current directory
    return glob.glob('collected_data_*.csv')

# Main App
def main():
    st.set_page_config(page_title="Frijoles", layout="wide")
    st.sidebar.title("Common Bean Data Collection App")
    st.sidebar.header("Please Log In")

    username = st.sidebar.text_input("Username")
    password = st.sidebar.text_input("Password", type="password")

    if st.sidebar.button("Login"):
        if check_credentials(username, password):
            st.session_state['authenticated'] = True
        else:
            st.session_state['authenticated'] = False
            st.sidebar.error("The username or password you entered is incorrect.")

    if 'authenticated' not in st.session_state:
        st.session_state['authenticated'] = False

    if st.session_state['authenticated']:
        st.title('Welcome to Common Bean Data Collection App')

        # Form for new data collection
        st.header("Collect new data")
        with st.form("new_data_form"):
            # Collect necessary information from user
            disease_type = st.selectbox("Select disease type", ['ALS', 'CBB'])
            region = st.selectbox("Select region", ['Selian', 'Uyole', 'Maluku'])
            genotype = st.selectbox("Select genotype", ['Genotype A', 'Genotype B'])
            growth_level = st.selectbox("Select plant growth level", ['First Trifoliate', 'Second Trifoliate'])
            plant_number = st.selectbox("Select plant number", list(range(1, 11)))
            leaf_number = st.selectbox("Select leaf number", list(range(1, 11)))
            scan_number = st.selectbox("Select scan number", list(range(1, 11)))
            visual_score = st.slider("Visual Score", 1, 9, 1)
            
            submitted = st.form_submit_button("Save")

            if submitted:
                # Construct the form data to save
                form_data = {
                    "Date": datetime.now().strftime("%Y-%m-%d"),
                    "Time": datetime.now().strftime("%H:%M:%S"),
                    "Disease Type": disease_type,
                    "Region": region,
                    "Genotype": genotype,
                    "Growth Level": growth_level,
                    "Plant Number": plant_number,
                    "Leaf Number": leaf_number,
                    "Scan Number": scan_number,
                    "Visual Score": visual_score
                }
                save_data(form_data, username)
                st.success("Data saved successfully!")

        # Button to open ScanSpectrum app (Placeholder for actual URL scheme)
        st.markdown("Please use your mobile device to open the ScanSpectrum app and perform the scan.")
        st.markdown("<a href='ScanSpectrum://'><button style='height: 50px; width: 300px; font-size: 20px;'>Open ScanSpectrum App</button></a>", unsafe_allow_html=True)

        # Functionality to preview CSV files
        st.header("Preview Saved Data")
        all_csv_files = list_csv_files()
        selected_csv = st.selectbox("Select a CSV file to preview", all_csv_files)
        
        if st.button('Show Selected CSV'):
            if selected_csv:
                data_to_show = pd.read_csv(selected_csv)
                st.write(f"Preview of {selected_csv}:")
                st.dataframe(data_to_show)
            else:
                st.error("No CSV file selected.")

    else:
        st.warning("You need to login to access the data collection form.")

# Run the main app
if __name__ == "__main__":
    main()