joshuadunlop commited on
Commit
bb98c0f
·
verified ·
1 Parent(s): 7319d9f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import requests
4
+
5
+ # Function to create a custom field in Maropost
6
+ def create_custom_field(data, auth_token):
7
+ headers = {
8
+ "Content-Type": "application/json",
9
+ "Accept": "application/json",
10
+ }
11
+ response = requests.post(
12
+ "https://api.maropost.com/accounts/1783/custom_fields.json",
13
+ json=data,
14
+ headers=headers,
15
+ params={'auth_token': auth_token}
16
+ )
17
+ return response
18
+
19
+ # Function to read CSV and create custom fields
20
+ def create_custom_fields_from_csv(csv_file, auth_token):
21
+ df = pd.read_csv(csv_file)
22
+ success_fields = []
23
+ failed_fields = []
24
+
25
+ for _, row in df.iterrows():
26
+ data = {
27
+ "custom_field": {
28
+ "name": row['name'],
29
+ "field_type": row['field_type'],
30
+ "add_to_profile_page": row.get('add_to_profile_page', False),
31
+ "default_value": row.get('default_value', "")
32
+ }
33
+ }
34
+ response = create_custom_field(data, auth_token)
35
+ if response.status_code in [200, 201]:
36
+ success_fields.append(row['name'])
37
+ else:
38
+ failed_fields.append((row['name'], response.status_code, response.text))
39
+
40
+ return success_fields, failed_fields
41
+
42
+ # Streamlit UI
43
+ def main():
44
+ st.title("Maropost Custom Fields Creator")
45
+
46
+ auth_token = st.text_input("Enter your Maropost API auth token:", type="password")
47
+ csv_file = st.file_uploader("Upload CSV file for custom fields", type=['csv'])
48
+
49
+ if st.button("Create Custom Fields"):
50
+ if not auth_token or csv_file is None:
51
+ st.error("Please enter your auth token and upload a CSV file.")
52
+ else:
53
+ success_fields, failed_fields = create_custom_fields_from_csv(csv_file, auth_token)
54
+ if success_fields:
55
+ st.success(f"Successfully created custom fields: {', '.join(success_fields)}.")
56
+ if failed_fields:
57
+ for failed_field, status_code, message in failed_fields:
58
+ st.error(f"Failed to create custom field {failed_field}. Response code: {status_code}, Message: {message}")
59
+
60
+ if __name__ == "__main__":
61
+ main()