Skip to content

Commit

Permalink
Add script to import attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
leonkenneth committed Feb 4, 2025
1 parent 51cc26a commit 5ae150c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app/scripts/import_diffusiondatabase_attributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import csv
import requests
from io import StringIO
from batid.models import DiffusionDatabase


def run(self, *args):
print(args)
csv_url = args[0]
target_database_id = args[1]

target_database = DiffusionDatabase.objects.get(id=int(target_database_id))

try:
response = requests.get(csv_url)
response.encoding = response.apparent_encoding
response.raise_for_status()

csv_file = StringIO(response.text)
reader = csv.DictReader(csv_file)

attributes = []
for row in reader:
attributes.append(row)

target_database.attributes = attributes
target_database.save()

print(f"Successfully imported {len(attributes)} attributes.")

except requests.RequestException as e:
print(f"Failed to fetch CSV: {e}")
except Exception as e:
print(f"Error processing CSV: {e}")

0 comments on commit 5ae150c

Please sign in to comment.