-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_functions.py
139 lines (106 loc) · 4.79 KB
/
io_functions.py
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
import pandas as pd
import boto3
import io
import json
import botocore
import streamlit as st
from PIL import Image
import plotly.io as pio
def get_file(files, file_string):
for file in files:
if file_string in file:
return file
# Only print the not found message if no file is found after checking all files
print(f"File {file_string} not found, skipping.")
return None
def upload_df_to_s3(df, filename, prefix, bucket_name='cup.clue.io'):
csv_buffer = io.StringIO()
df.to_csv(csv_buffer, index=False)
s3.put_object(Body=csv_buffer.getvalue().encode('utf-8'), Bucket=bucket_name, Key=f"{prefix}/{filename}")
print(f"File '{filename}' uploaded to bucket '{bucket_name}'")
def load_df_from_s3(filename, prefix, bucket_name='cup.clue.io'):
s3 = boto3.client('s3')
try:
# Check if the object exists by retreiving metadata
s3.head_object(Bucket=bucket_name, Key=f"{prefix}/{filename}")
# If exustsm proceed
response = s3.get_object(Bucket=bucket_name, Key=f"{prefix}/{filename}")
csv_bytes = response['Body'].read()
csv_buffer = io.StringIO(csv_bytes.decode())
df = pd.read_csv(csv_buffer)
return df
except botocore.exceptions.ClientError as e:
st.text('There is at least one file missing, please regenerate report and try again.')
def load_json_table_from_s3(filename, prefix, bucket_name='cup.clue.io'):
s3 = boto3.client('s3')
try:
# Check if the object exists by retreiving metadata
s3.head_object(Bucket=bucket_name, Key=f"{prefix}/{filename}")
# If no exceptions, proceed
response = s3.get_object(Bucket=bucket_name, Key=f"{prefix}/{filename}")
json_bytes = response['Body'].read()
json_str = json_bytes.decode()
df = pd.read_json(io.StringIO(json_str), orient='records')
return df
except botocore.exceptions.ClientError as e:
st.text('There is at least one file missing, please regenerate report and try again.')
def load_plot_from_s3(filename, prefix, bucket_name='cup.clue.io'):
s3 = boto3.client('s3')
try:
# Check if the object exists by retreiving metadata
s3.head_object(Bucket=bucket_name, Key=f"{prefix}/{filename}")
# If no exception, proceed
response = s3.get_object(Bucket=bucket_name, Key=f"{prefix}/{filename}")
fig_json = response['Body'].read().decode('utf-8')
fig = pio.from_json(fig_json)
st.plotly_chart(fig)
except botocore.exceptions.ClientError as e:
st.text('There is at least one file missing, please regenerate report and try again.')
def load_image_from_s3(filename, prefix, bucket_name='cup.clue.io'):
s3 = boto3.client('s3')
try:
# Check if the object exists by retrieving metadata
s3.head_object(Bucket=bucket_name, Key=f"{prefix}/{filename}")
print(f"File '{filename}' found in bucket '{bucket_name}'")
# If the previous line didn't raise an exception, proceed with fetching the actual object
response = s3.get_object(Bucket=bucket_name, Key=f"{prefix}/{filename}")
content = response['Body'].read()
# Load image data from buffer
img_buffer = io.BytesIO(content)
img = Image.open(img_buffer)
# Display image in Streamlit
st.image(img)
except botocore.exceptions.ClientError as e:
st.text('There is at least one file missing, please regenerate report and try again.')
def check_file_exists(bucket_name, file_name):
s3 = boto3.client('s3')
try:
s3.head_object(Bucket=bucket_name, Key=file_name)
return True
except Exception as e:
return False
def write_json_to_s3(bucket, filename, data, prefix):
s3 = boto3.client('s3')
# Convert your data to a JSON string
json_data = json.dumps(data)
# Convert your JSON string to bytes
json_bytes = json_data.encode()
# Write the JSON data to an S3 object
s3.put_object(Body=json_bytes, Bucket=bucket, Key=f"{prefix}/{filename}")
def write_json_table_to_s3(bucket, filename, data, prefix):
s3 = boto3.client('s3')
# Convert your JSON string to bytes
json_bytes = data.encode()
# Write the JSON data to an S3 object with specified content type
s3.put_object(Body=json_bytes, Bucket=bucket, Key=f"{prefix}/{filename}", ContentType='application/json')
def read_json_from_s3(bucket_name, filename, prefix):
s3 = boto3.client('s3')
# Get the object from S3
s3_object = s3.get_object(Bucket=bucket_name, Key=f"{prefix}/{filename}")
# Get the body of the object (the data content)
s3_object_body = s3_object['Body'].read()
# Convert bytes to string
string_data = s3_object_body.decode('utf-8')
# Convert string data to dictionary
dict_data = json.loads(string_data)
return dict_data