Skip to content

Commit

Permalink
feature(backend) integrating analysis script with backend api correction
Browse files Browse the repository at this point in the history
  • Loading branch information
bansikah22 committed Nov 29, 2023
1 parent 4f2e1dd commit 27ff4b7
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 41 deletions.
17 changes: 17 additions & 0 deletions api-analysis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from flask import Flask, request, jsonify
from app import performm_analyses

app = Flask(__name__)

# #Developing an api end point that will trigger the analysis script
@app.route('/api/analyze', methods=['POST'])
def analyze_data():
# Get the data from the request
data = request.get_data(as_text=True)
# Perform analyses
results = perform_analyses(data)
# Return the analysis results as JSON
return jsonify(results)

if __name__ == '__main__':
app.run(debug=True)
11 changes: 0 additions & 11 deletions templates/test.html
Original file line number Diff line number Diff line change
@@ -1,11 +0,0 @@
{% extends 'base.html' %}

{% set active_page = "results" %}

{% block content %}
<h1>Get your analysis here!</h1>
{% endblock %}

{% block title %}
Results
{% endblock %}
85 changes: 85 additions & 0 deletions test.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"from flask import Flask, request, jsonify\n",
"import json\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"from io import StringIO\n",
"import csv"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"app = Flask(__name__)\n",
"\n",
"def perform_analyses(data):\n",
" try:\n",
" is_json = False\n",
" is_csv = False\n",
" # Try to parse as JSON\n",
" try:\n",
" data = json.loads(data)\n",
" is_json = True\n",
" except json.JSONDecodeError:\n",
" pass\n",
" # If JSON parsing failed, try parsing as CSV\n",
" if not is_json:\n",
" try:\n",
" # If the data has headers\n",
" data = list(csv.DictReader(StringIO(data)))\n",
" is_csv = True\n",
" except csv.Error:\n",
" pass\n",
" if not is_json and not is_csv:\n",
" raise ValueError(\"The data format is not supported.\")\n",
" if is_json and not isinstance(data, list):\n",
" raise ValueError(\"The data should be formatted as a list of objects.\")\n",
" df = pd.DataFrame(data)\n",
" # Perform analyses\n",
" total_websites = df.shape[0]\n",
" average_char_count = df['char_count'].mean()\n",
" average_image_count = df['image_count'].mean()\n",
" # Generate histograms\n",
" char_count_hist = df['char_count'].hist(bins=50).get_figure()\n",
" image_count_hist = df['image_count'].hist(bins=50).get_figure()\n",
" # Save histograms as images\n",
" char_count_hist.savefig('char_count_hist.png')\n",
" image_count_hist.savefig('image_count_hist.png')\n",
" # Return analysis results\n",
" return {\n",
" 'total_websites': total_websites,\n",
" 'average_char_count': average_char_count,\n",
" 'average_image_count': average_image_count\n",
" }\n",
" except ValueError as e:\n",
" return str(e)\n",
"\n",
"@app.route('/api/analyze', methods=['POST'])\n",
"def analyze_data():\n",
" # Get the data from the request\n",
" data = request.get_data(as_text=True)\n",
" # Perform analyses\n",
" results = perform_analyses(data)\n",
" # Return the analysis results as JSON\n",
" return jsonify(results)\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
30 changes: 0 additions & 30 deletions test/test_analysis.py

This file was deleted.

0 comments on commit 27ff4b7

Please sign in to comment.