Ninad077 commited on
Commit
b88b965
Β·
verified Β·
1 Parent(s): 1e46376

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +11 -1
  2. bq.py +327 -0
app.py CHANGED
@@ -38,6 +38,9 @@ EMAIL_HOST_USER = '[email protected]'
38
  EMAIL_HOST_PASSWORD = 'vxay jiss cctw lsdo'
39
 
40
 
 
 
 
41
  html_title = """
42
  <style>
43
  .fixed-title {
@@ -58,7 +61,8 @@ st.markdown(html_title, unsafe_allow_html=True)
58
  menu_options = [
59
  {"label": "Internal users", "icon": "πŸ“„", "description": "Upload a document and schedule it for email"},
60
  {"label": "External users", "icon": "πŸ“Š", "description": "Schedule an email with BigQuery data"},
61
- {"label": "Recon checking", "icon": "πŸ“Š", "description": "Schedule an email with BigQuery data"}
 
62
  ]
63
 
64
  # Create the custom option menu
@@ -384,3 +388,9 @@ elif selected_option == "Internal users":
384
  elif selected_option == "Recon checking":
385
  with open('recon.py') as file:
386
  exec(file.read())
 
 
 
 
 
 
 
38
  EMAIL_HOST_PASSWORD = 'vxay jiss cctw lsdo'
39
 
40
 
41
+ st.set_page_config(page_title="Alerter",page_icon="",layout="centered")
42
+
43
+
44
  html_title = """
45
  <style>
46
  .fixed-title {
 
61
  menu_options = [
62
  {"label": "Internal users", "icon": "πŸ“„", "description": "Upload a document and schedule it for email"},
63
  {"label": "External users", "icon": "πŸ“Š", "description": "Schedule an email with BigQuery data"},
64
+ {"label": "Recon checking", "icon": "πŸ“Š", "description": "Schedule an email with BigQuery data"},
65
+ {"label": "Manual BQ upload", "icon": "πŸ“Š", "description": "Schedule an email with BigQuery data"}
66
  ]
67
 
68
  # Create the custom option menu
 
388
  elif selected_option == "Recon checking":
389
  with open('recon.py') as file:
390
  exec(file.read())
391
+
392
+ elif selected_option == "Manual BQ upload":
393
+ with open('bq.py') as file:
394
+ exec(file.read())
395
+
396
+
bq.py ADDED
@@ -0,0 +1,327 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pandas as pd
3
+ import streamlit as st
4
+ from google.cloud import bigquery
5
+
6
+ def preprocess_csv(file_path):
7
+ # Load the CSV file
8
+ df = pd.read_csv(file_path)
9
+
10
+ # Define columns to be converted
11
+ date_columns = ['Order_Date', 'State_Date', 'Entry_Month']
12
+
13
+ # Convert specified columns from DD/MM/YY to 'YYYY-MM-DD 00:00:00 UTC'
14
+ for column in date_columns:
15
+ if column in df.columns:
16
+ df[column] = pd.to_datetime(df[column], format='%d/%m/%y', errors='coerce').dt.strftime('%Y-%m-%d 00:00:00 UTC')
17
+
18
+ # Save the preprocessed CSV
19
+ preprocessed_file_path = 'preprocessed_' + os.path.basename(file_path)
20
+ df.to_csv(preprocessed_file_path, index=False)
21
+
22
+ return preprocessed_file_path
23
+
24
+ def upload_to_bigquery(credentials_path, csv_file_path, project_name, table_id):
25
+ try:
26
+ # Set up the BigQuery client
27
+ os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = credentials_path
28
+ client = bigquery.Client(project=project_name)
29
+
30
+ # Retrieve the existing table schema
31
+ table = client.get_table(table_id)
32
+ schema = table.schema
33
+
34
+ # Prepare the BigQuery job configuration with the existing schema
35
+ job_config = bigquery.LoadJobConfig(
36
+ source_format=bigquery.SourceFormat.CSV,
37
+ skip_leading_rows=1, # Assuming the first row is the header
38
+ schema=schema, # Use the existing schema from BigQuery
39
+ write_disposition=bigquery.WriteDisposition.WRITE_APPEND # Append to the table
40
+ )
41
+
42
+ # Load the preprocessed CSV data into BigQuery
43
+ with open(csv_file_path, "rb") as file:
44
+ load_job = client.load_table_from_file(
45
+ file,
46
+ table_id,
47
+ job_config=job_config
48
+ )
49
+
50
+ # Wait for the load job to complete
51
+ load_job.result()
52
+
53
+ # Check the result
54
+ destination_table = client.get_table(table_id)
55
+ st.success(f"Loaded {destination_table.num_rows} rows into {table_id}.")
56
+
57
+ except Exception as e:
58
+ st.error(f"An error occurred: {str(e)}")
59
+
60
+ html_subject = """
61
+ <html>
62
+ <head>
63
+ <style>
64
+ .button {
65
+ display: inline-block;
66
+ padding: 10px 20px;
67
+ border-radius: 12px;
68
+ background: linear-gradient(to bottom, #f8f9fa, #e0e0e0);
69
+ box-shadow:
70
+ 0 6px 12px rgba(0, 0, 0, 0.3),
71
+ 0 8px 16px rgba(0, 0, 0, 0.2),
72
+ inset 0 -2px 4px rgba(255, 255, 255, 0.6);
73
+ text-align: center;
74
+ position: relative;
75
+ transform: translateY(4px);
76
+ transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
77
+ cursor: pointer;
78
+ user-select: none;
79
+ }
80
+ .button:hover {
81
+ box-shadow:
82
+ 0 8px 16px rgba(0, 0, 0, 0.3),
83
+ 0 12px 24px rgba(0, 0, 0, 0.2);
84
+ transform: translateY(2px);
85
+ }
86
+ .button:active {
87
+ box-shadow:
88
+ 0 4px 8px rgba(0, 0, 0, 0.3),
89
+ 0 6px 12px rgba(0, 0, 0, 0.2);
90
+ transform: translateY(0);
91
+ }
92
+ </style>
93
+ </head>
94
+ <body>
95
+ <div class="button">
96
+ <h3 style="
97
+ font-size: 20px;
98
+ color: #ffffff;
99
+ background-image: linear-gradient(to right, #800000, #ff0000, #ffdab9);
100
+ background-clip: text;
101
+ -webkit-background-clip: text;
102
+ text-fill-color: transparent;
103
+ -webkit-text-fill-color: transparent;
104
+ margin: 0;
105
+ text-shadow: 0 2px 5px rgba(0, 0, 0, 0.4);
106
+ ">Enter project name</h3>
107
+ </div>
108
+ </body>
109
+ </html>
110
+ """
111
+
112
+ st.markdown(html_subject, unsafe_allow_html=True)
113
+
114
+ # Input fields for Project Name and Table ID
115
+ project_name = st.text_input("",key ="project_name")
116
+
117
+
118
+ html_subject = """
119
+ <html>
120
+ <head>
121
+ <style>
122
+ .button {
123
+ display: inline-block;
124
+ padding: 10px 20px;
125
+ border-radius: 12px;
126
+ background: linear-gradient(to bottom, #f8f9fa, #e0e0e0);
127
+ box-shadow:
128
+ 0 6px 12px rgba(0, 0, 0, 0.3),
129
+ 0 8px 16px rgba(0, 0, 0, 0.2),
130
+ inset 0 -2px 4px rgba(255, 255, 255, 0.6);
131
+ text-align: center;
132
+ position: relative;
133
+ transform: translateY(4px);
134
+ transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
135
+ cursor: pointer;
136
+ user-select: none;
137
+ }
138
+ .button:hover {
139
+ box-shadow:
140
+ 0 8px 16px rgba(0, 0, 0, 0.3),
141
+ 0 12px 24px rgba(0, 0, 0, 0.2);
142
+ transform: translateY(2px);
143
+ }
144
+ .button:active {
145
+ box-shadow:
146
+ 0 4px 8px rgba(0, 0, 0, 0.3),
147
+ 0 6px 12px rgba(0, 0, 0, 0.2);
148
+ transform: translateY(0);
149
+ }
150
+ </style>
151
+ </head>
152
+ <body>
153
+ <div class="button">
154
+ <h3 style="
155
+ font-size: 20px;
156
+ color: #ffffff;
157
+ background-image: linear-gradient(to right, #800000, #ff0000, #ffdab9);
158
+ background-clip: text;
159
+ -webkit-background-clip: text;
160
+ text-fill-color: transparent;
161
+ -webkit-text-fill-color: transparent;
162
+ margin: 0;
163
+ text-shadow: 0 2px 5px rgba(0, 0, 0, 0.4);
164
+ ">Enter table id</h3>
165
+ </div>
166
+ </body>
167
+ </html>
168
+ """
169
+
170
+ st.markdown(html_subject, unsafe_allow_html=True)
171
+ table_id = st.text_input("", key= "table_id_name")
172
+
173
+ # File uploader for credentials.json
174
+
175
+ html_subject = """
176
+ <html>
177
+ <head>
178
+ <style>
179
+ .button {
180
+ display: inline-block;
181
+ padding: 10px 20px;
182
+ border-radius: 12px;
183
+ background: linear-gradient(to bottom, #f8f9fa, #e0e0e0);
184
+ box-shadow:
185
+ 0 6px 12px rgba(0, 0, 0, 0.3),
186
+ 0 8px 16px rgba(0, 0, 0, 0.2),
187
+ inset 0 -2px 4px rgba(255, 255, 255, 0.6);
188
+ text-align: center;
189
+ position: relative;
190
+ transform: translateY(4px);
191
+ transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
192
+ cursor: pointer;
193
+ user-select: none;
194
+ }
195
+ .button:hover {
196
+ box-shadow:
197
+ 0 8px 16px rgba(0, 0, 0, 0.3),
198
+ 0 12px 24px rgba(0, 0, 0, 0.2);
199
+ transform: translateY(2px);
200
+ }
201
+ .button:active {
202
+ box-shadow:
203
+ 0 4px 8px rgba(0, 0, 0, 0.3),
204
+ 0 6px 12px rgba(0, 0, 0, 0.2);
205
+ transform: translateY(0);
206
+ }
207
+ </style>
208
+ </head>
209
+ <body>
210
+ <div class="button">
211
+ <h3 style="
212
+ font-size: 20px;
213
+ color: #ffffff;
214
+ background-image: linear-gradient(to right, #800000, #ff0000, #ffdab9);
215
+ background-clip: text;
216
+ -webkit-background-clip: text;
217
+ text-fill-color: transparent;
218
+ -webkit-text-fill-color: transparent;
219
+ margin: 0;
220
+ text-shadow: 0 2px 5px rgba(0, 0, 0, 0.4);
221
+ ">Upload credentials file</h3>
222
+ </div>
223
+ </body>
224
+ </html>
225
+ """
226
+
227
+ st.markdown(html_subject, unsafe_allow_html=True)
228
+ credentials_file = st.file_uploader("", type=["json"])
229
+
230
+ # File uploader for the CSV file
231
+
232
+ html_subject = """
233
+ <html>
234
+ <head>
235
+ <style>
236
+ .button {
237
+ display: inline-block;
238
+ padding: 10px 20px;
239
+ border-radius: 12px;
240
+ background: linear-gradient(to bottom, #f8f9fa, #e0e0e0);
241
+ box-shadow:
242
+ 0 6px 12px rgba(0, 0, 0, 0.3),
243
+ 0 8px 16px rgba(0, 0, 0, 0.2),
244
+ inset 0 -2px 4px rgba(255, 255, 255, 0.6);
245
+ text-align: center;
246
+ position: relative;
247
+ transform: translateY(4px);
248
+ transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
249
+ cursor: pointer;
250
+ user-select: none;
251
+ }
252
+ .button:hover {
253
+ box-shadow:
254
+ 0 8px 16px rgba(0, 0, 0, 0.3),
255
+ 0 12px 24px rgba(0, 0, 0, 0.2);
256
+ transform: translateY(2px);
257
+ }
258
+ .button:active {
259
+ box-shadow:
260
+ 0 4px 8px rgba(0, 0, 0, 0.3),
261
+ 0 6px 12px rgba(0, 0, 0, 0.2);
262
+ transform: translateY(0);
263
+ }
264
+ </style>
265
+ </head>
266
+ <body>
267
+ <div class="button">
268
+ <h3 style="
269
+ font-size: 20px;
270
+ color: #ffffff;
271
+ background-image: linear-gradient(to right, #800000, #ff0000, #ffdab9);
272
+ background-clip: text;
273
+ -webkit-background-clip: text;
274
+ text-fill-color: transparent;
275
+ -webkit-text-fill-color: transparent;
276
+ margin: 0;
277
+ text-shadow: 0 2px 5px rgba(0, 0, 0, 0.4);
278
+ ">Upload the CSV file</h3>
279
+ </div>
280
+ </body>
281
+ </html>
282
+ """
283
+
284
+ st.markdown(html_subject, unsafe_allow_html=True)
285
+ uploaded_file = st.file_uploader("", type=["csv"])
286
+
287
+ # Submit button
288
+ button_styles = """
289
+ <style>
290
+ div.stButton > button {
291
+ color: #ffffff; /* Text color */
292
+ font-size: 30px;
293
+ background-image: linear-gradient(to right, #800000, #ff0000); /* Maroon to light red gradient */
294
+ border: none;
295
+ padding: 10px 20px;
296
+ cursor: pointer;
297
+ border-radius: 15px;
298
+ display: inline-block;
299
+ box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1), 0 8px 15px rgba(0, 0, 0, 0.1); /* Box shadow */
300
+ transition: all 0.3s ease; /* Smooth transition on hover */
301
+ }
302
+ div.stButton > button:hover {
303
+ background-color: #00ff00; /* Hover background color */
304
+ color: #ff0000; /* Hover text color */
305
+ box-shadow: 0 6px 10px rgba(0, 0, 0, 0.2), 0 12px 20px rgba(0, 0, 0, 0.2); /* Box shadow on hover */
306
+ }
307
+ </style>
308
+ """
309
+ st.markdown(button_styles, unsafe_allow_html=True)
310
+ if st.button("Submit"):
311
+ if uploaded_file is not None and credentials_file is not None and project_name and table_id:
312
+ # Save the uploaded files to the current directory
313
+ credentials_path = "./credentials.json"
314
+ with open(credentials_path, "wb") as f:
315
+ f.write(credentials_file.getbuffer())
316
+
317
+ csv_file_path = f"./{uploaded_file.name}"
318
+ with open(csv_file_path, "wb") as f:
319
+ f.write(uploaded_file.getbuffer())
320
+
321
+ # Preprocess the CSV file (convert date formats)
322
+ preprocessed_file_path = preprocess_csv(csv_file_path)
323
+
324
+ # Upload the preprocessed CSV file to BigQuery
325
+ upload_to_bigquery(credentials_path, preprocessed_file_path, project_name, table_id)
326
+ else:
327
+ st.error("Please fill out all fields, including the credentials file and CSV file.")