File size: 6,781 Bytes
b18eae1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167b433
b18eae1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a32ee0
b18eae1
 
 
 
 
 
 
 
 
 
 
 
 
 
f416a92
 
 
 
 
 
 
 
 
 
b18eae1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import gradio as gr
import psycopg2
from dataclasses import dataclass, field
from typing import List, Optional
from mysite.interpreter.process import no_process_file,process_file

@dataclass
class Ride:
    ride_id: Optional[int] = field(default=None)
    rideable_type: str = ''
    start_station_id: int = 0
    start_station_name: str = ''
    end_station_id: int = 0
    end_station_name: str = ''
    started_at: str = ''
    ended_at: str = ''
    member_casual: str = ''

def connect_to_db():
    conn = psycopg2.connect(
        dbname="neondb",
        user="miyataken999",
        password="yz1wPf4KrWTm",
        host="ep-odd-mode-93794521.us-east-2.aws.neon.tech",
        port=5432,
        sslmode="require"
    )
    return conn

def create_ride(ride: Ride):
    conn = connect_to_db()
    cur = conn.cursor()
    cur.execute("INSERT INTO rides (rideable_type, start_station_id, start_station_name, end_station_id, end_station_name, started_at, ended_at, member_casual) VALUES (%s, %s, %s, %s, %s, %s, %s, %s) RETURNING ride_id",
               (ride.rideable_type, ride.start_station_id, ride.start_station_name, ride.end_station_id, ride.end_station_name, ride.started_at, ride.ended_at, ride.member_casual))
    ride_id = cur.fetchone()[0]
    conn.commit()
    cur.close()
    conn.close()
    return ride_id

def read_rides():
    conn = connect_to_db()
    cur = conn.cursor()
    cur.execute("SELECT * FROM rides ORDER BY ride_id desc")
    rides = cur.fetchall()
    conn.close()
    return rides

def read_ride(ride_id: int):
    conn = connect_to_db()
    cur = conn.cursor()
    cur.execute("SELECT * FROM rides WHERE ride_id = %s", (ride_id,))
    ride = cur.fetchone()
    conn.close()
    return ride

def update_ride(ride: Ride):
    conn = connect_to_db()
    cur = conn.cursor()
    no_process_file(ride.start_station_name,ride.end_station_name)
    cur.execute("UPDATE rides SET rideable_type = %s, start_station_id = %s, start_station_name = %s, end_station_id = %s, end_station_name = %s, started_at = %s, ended_at = %s, member_casual = %s WHERE ride_id = %s",
               (ride.rideable_type, ride.start_station_id, ride.start_station_name, ride.end_station_id, ride.end_station_name, ride.started_at, ride.ended_at, ride.member_casual, ride.ride_id))
    conn.commit()
    cur.close()
    conn.close()

def delete_ride(ride_id: int):
    conn = connect_to_db()
    cur = conn.cursor()
    cur.execute("DELETE FROM rides WHERE ride_id = %s", (ride_id,))
    conn.commit()
    cur.close()
    conn.close()

def test_set_lide(input="test",foldername="test"):
    ride = Ride(
        rideable_type="rideable_type",
        start_station_name=input,
        end_station_name=foldername,
    )
    create_ride(ride)
    return [[r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8]] for r in read_rides()]


#def crud_interface():
with gr.Blocks() as gradio_interface:
    with gr.Row():
        rideable_type = gr.Textbox(label="Rideable Type")
        start_station_id = gr.Number(label="Start Station ID")
        start_station_name = gr.Textbox(label="Start Station Name")
        end_station_id = gr.Number(label="End Station ID")
        end_station_name = gr.Textbox(label="End Station Name")
        started_at = gr.Textbox(label="Started At")
        ended_at = gr.Textbox(label="Ended At")
        member_casual = gr.Textbox(label="Member Casual")
        ride_id = gr.Number(label="Ride ID (for update/delete)", value=-1, interactive=False)

    create_button = gr.Button("Create Ride")
    update_button = gr.Button("Update Ride")
    delete_button = gr.Button("Delete Ride")
    read_button = gr.Button("Read Rides")
    output = gr.Dataframe(headers=["Ride ID", "Rideable Type", "Start Station ID", "Start Station Name", "End Station ID", "End Station Name", "Started At", "Ended At", "Member Casual"])

    def create_ride_click(rideable_type, start_station_id, start_station_name, end_station_id, end_station_name, started_at, ended_at, member_casual):
        ride = Ride(
            rideable_type=rideable_type,
            start_station_id=start_station_id,
            start_station_name=start_station_name,
            end_station_id=end_station_id,
            end_station_name=end_station_name,
            started_at=started_at,
            ended_at=ended_at,
            member_casual=member_casual
        )
        create_ride(ride)
        return [[r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8]] for r in read_rides()]

    def update_ride_click(ride_id, rideable_type, start_station_id, start_station_name, end_station_id, end_station_name, started_at, ended_at, member_casual):
        ride = Ride(
            ride_id=int(ride_id),
            rideable_type=rideable_type,
            start_station_id=start_station_id,
            start_station_name=start_station_name,
            end_station_id=end_station_id,
            end_station_name=end_station_name,
            started_at=started_at,
            ended_at=ended_at,
            member_casual=member_casual
        )
        update_ride(ride)
        return [[r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8]] for r in read_rides()]

    def delete_ride_click(ride_id):
        delete_ride(int(ride_id))
        return [[r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8]] for r in read_rides()]

    def read_rides_click():
        return [[r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8]] for r in read_rides()]

    def load_ride_details(evt: gr.SelectData):
        row_index = evt.index[0] if isinstance(evt.index, list) else evt.index
        rides = read_rides()
        selected_row = rides[row_index]
        ride_id = selected_row[0]
        ride = read_ride(ride_id)
        if ride:
            return ride[1], ride[2], ride[3], ride[4], ride[5], ride[6], ride[7], ride[8], ride[0]
        return "", 0, "", 0, "", "", "", "", -1

    create_button.click(fn=create_ride_click, 
                        inputs=[rideable_type, start_station_id, start_station_name, end_station_id, end_station_name, started_at, ended_at, member_casual], 
                        outputs=output)
    update_button.click(fn=update_ride_click, 
                        inputs=[ride_id, rideable_type, start_station_id, start_station_name, end_station_id, end_station_name, started_at, ended_at, member_casual], 
                        outputs=output)
    delete_button.click(fn=delete_ride_click, inputs=ride_id, outputs=output)
    read_button.click(fn=read_rides_click, outputs=output)
    
    output.select(fn=load_ride_details, inputs=None, outputs=[rideable_type, start_station_id, start_station_name, end_station_id, end_station_name, started_at, ended_at, member_casual, ride_id])

#return interface

#d1 = crud_interface()
#d1.launch()