-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(backend) integrating analysis script with backend api correction
- Loading branch information
1 parent
4f2e1dd
commit 27ff4b7
Showing
4 changed files
with
102 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.