Spaces:
Runtime error
Runtime error
import streamlit as st | |
import pandas as pd | |
import requests | |
# Function to create a list in Maropost | |
def create_list(data, auth_token): | |
headers = { | |
"Content-Type": "application/json", | |
"Accept": "application/json", | |
} | |
response = requests.post( | |
"https://api.maropost.com/accounts/1783/lists.json", | |
json=data, | |
headers=headers, | |
params={'auth_token': auth_token} | |
) | |
return response | |
# Function to read CSV and create lists | |
def create_lists_from_csv(csv_file, auth_token): | |
df = pd.read_csv(csv_file) | |
success_lists = [] | |
failed_lists = [] | |
for _, row in df.iterrows(): | |
data = { | |
"list": { | |
"reply_to_email": row['reply-to-email'], | |
"address": row['address'], | |
"from_email": row['from-email'], | |
"language": row['language'], | |
"from_name": row.get('from-name', ""), | |
"name": row['name'], | |
"add_to_unsubscribe_page": row.get('add-to-unsubscribe-page', False), | |
"post_url": None, # Assuming post_url will always be None as per your example | |
"display_name": row.get('display-name', ""), | |
"description": row.get('description', "") | |
} | |
} | |
response = create_list(data, auth_token) | |
if response.status_code in [200, 201]: | |
success_lists.append(row['name']) | |
else: | |
failed_lists.append((row['name'], response.status_code, response.text)) | |
return success_lists, failed_lists | |
# Streamlit UI | |
def main(): | |
st.title("Maropost Lists Creator") | |
auth_token = st.text_input("Enter your Maropost API auth token:", type="password") | |
csv_file = st.file_uploader("Upload CSV file for lists", type=['csv']) | |
if st.button("Create Lists"): | |
if not auth_token or csv_file is None: | |
st.error("Please enter your auth token and upload a CSV file.") | |
else: | |
success_lists, failed_lists = create_lists_from_csv(csv_file, auth_token) | |
if success_lists: | |
st.success(f"Successfully created lists: {', '.join(success_lists)}.") | |
if failed_lists: | |
for failed_list, status_code, message in failed_lists: | |
st.error(f"Failed to create list {failed_list}. Response code: {status_code}, Message: {message}") | |
if __name__ == "__main__": | |
main() | |