it takes a while to load all the data into the new df_with_ṕredictions, my problem is how can I render the new table with those predictions in a template :
def home(request):
df = None
df_with_predictions = None
error_message = None
api_key = None
column_name = None
predictions_html = "" # Initialize an empty string for the DataFrame HTML
if request.method == 'POST':
api_key = request.POST.get('api_key_input', '')
column_name = request.POST.get('column_name', '')
file = request.FILES.get('file')
if file:
try:
file_ext = file.name.split(".")[-1]
if file_ext == "csv":
df = pd.read_csv(file)
elif file_ext == "xlsx":
df = pd.read_excel(file)
if df is not None:
x_new = get_embeddings(df, column_name, api_key)
if x_new is not None:
new_predictions = classify_comments(x_new)
df_with_predictions = df.copy()
df_with_predictions['Clasificación_gpt_'] = new_predictions
print("File has been processed and predictions are added. in home")
print(df_with_predictions)
# Convert DataFrame to HTML
predictions_html = df_with_predictions.to_html(classes="table table-striped", index=False)
except Exception as e:
error_message = str(e)
context = {
'api_key': api_key,
'column_name': column_name,
'predictions_html': predictions_html, # Pass the HTML to the template
'error_message': error_message
}
return render(request, "home.html", context)
I tried having al the variables in one post for manipulated in the home views, but I am still having troubles
New contributor
Luiz2002 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.