Skip to content

Commit

Permalink
testing csv export on railway
Browse files Browse the repository at this point in the history
  • Loading branch information
star-nox committed Nov 13, 2023
1 parent 2f03517 commit 01c962e
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 9 deletions.
20 changes: 14 additions & 6 deletions ai_ta_backend/export_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
import pandas as pd
import supabase
from flask import send_file

def export_convo_history_csv(course_name: str, from_date= '', to_date= ''):
"""
Expand Down Expand Up @@ -35,6 +36,7 @@ def export_convo_history_csv(course_name: str, from_date= '', to_date= ''):
print("id count greater than zero")
first_id = response.data[0]['id']
last_id = response.data[-1]['id']
filename = course_name + '_convo_history.csv'

# Fetch data in batches of 25 from first_id to last_id
while first_id <= last_id:
Expand All @@ -43,17 +45,23 @@ def export_convo_history_csv(course_name: str, from_date= '', to_date= ''):
# Convert to pandas dataframe
df = pd.DataFrame(response.data)
# Append to csv file
filename = course_name + '_convo_history.csv'
if not os.path.isfile(filename):
df.to_csv(filename, mode='a', header=True, index=False)
if not os.path.isfile('docs/' + filename):
df.to_csv('docs/' + filename, mode='a', header=True, index=False)
else:
df.to_csv(filename, mode='a', header=False, index=False)
df.to_csv('docs/' + filename, mode='a', header=False, index=False)

# Update first_id
first_id = response.data[-1]['id'] + 1
print("updated first_id: ", first_id)


# Download file
file_path = os.path.join(os.getcwd(), 'docs/' + filename)
try:
return (file_path, 'docs/' + filename, os.getcwd())
except Exception as e:
print(e)
return "Error downloading file"
else:
print("No data found between the dates")
return "No data found between the dates"
return "success"

12 changes: 9 additions & 3 deletions ai_ta_backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import List

from dotenv import load_dotenv
from flask import Flask, Response, abort, jsonify, request
from flask import Flask, Response, abort, jsonify, request, send_file, make_response, send_from_directory
from flask_cors import CORS
from flask_executor import Executor
from sqlalchemy import JSON
Expand Down Expand Up @@ -486,9 +486,15 @@ def export_convo_history():
)

export_status = export_convo_history_csv(course_name, from_date, to_date)

response = jsonify(export_status)
print(export_status)
print("path: ", os.getcwd() + '/docs/cropwizard_convo_history.csv')
print("path exists: ", os.path.exists(os.getcwd() + '/docs/cropwizard_convo_history.csv'))

response = make_response(send_file(os.getcwd() + '/docs/cropwizard_convo_history.csv', as_attachment=True))
#response = make_response(send_from_directory('docs', 'cropwizard_convo_history.csv', as_attachment=True))
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers["Content-Disposition"] = f"attachment; filename={export_status[1]}"

return response


Expand Down
172 changes: 172 additions & 0 deletions cropwizard_convo_history.csv

Large diffs are not rendered by default.

0 comments on commit 01c962e

Please sign in to comment.