diff --git a/api-analysis.py b/api-analysis.py new file mode 100644 index 0000000..8ce5997 --- /dev/null +++ b/api-analysis.py @@ -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) diff --git a/test.ipynb b/test.ipynb new file mode 100644 index 0000000..82f5bbf --- /dev/null +++ b/test.ipynb @@ -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 +}