Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature #39 backend:Integrate analysis script with backend API #90

Merged
merged 3 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
91 changes: 91 additions & 0 deletions test.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"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": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}