Skip to content

Commit

Permalink
Frontend propagates errors
Browse files Browse the repository at this point in the history
  • Loading branch information
KFilippopolitis committed Jun 25, 2024
1 parent 89debe1 commit 40c052c
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 82 deletions.
3 changes: 0 additions & 3 deletions data_quality_tool/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,3 @@ services:
dockerfile: Dockerfile
ports:
- "80:80"
environment:
- FLASK_ENV=development
- FLASK_DEBUG=1
94 changes: 50 additions & 44 deletions data_quality_tool/controller.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
from flask import Flask, request, jsonify, send_file
import json
import logging
from flask import Flask, request, jsonify, send_file
from flask_cors import CORS
import pandas as pd
from io import BytesIO
import pandas as pd
from flask_cors import CORS
from converter.excel_to_json import convert_excel_to_json
from converter.json_to_excel import convert_json_to_excel
from validator import json_validator, excel_validator

app = Flask(__name__)
# Allow all origins for all routes
CORS(app, resources={r"/*": {"origins": "*"}})

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

Expand All @@ -32,54 +30,62 @@ def excel_to_json():
logger.error("No selected file")
return jsonify({"error": "No selected file"}), 400
if file:
logger.info(f"Processing file: {file.filename}")
file_stream = BytesIO(file.read())
df = pd.read_excel(file_stream, engine="openpyxl")
logger.info("Excel file read into DataFrame")
excel_validator.validate_excel(df)
logger.info("Excel file validated")
json_data = convert_excel_to_json(df)
logger.info("Excel file converted to JSON")
pretty_json_data = json.dumps(json_data, indent=4)
logger.info("JSON data pretty-printed")
response = app.response_class(
response=pretty_json_data,
status=200,
mimetype='application/json'
)
return response
try:
logger.info(f"Processing file: {file.filename}")
file_stream = BytesIO(file.read())
df = pd.read_excel(file_stream, engine="openpyxl")
logger.info("Excel file read into DataFrame")
excel_validator.validate_excel(df)
logger.info("Excel file validated")
json_data = convert_excel_to_json(df)
logger.info("Excel file converted to JSON")
pretty_json_data = json.dumps(json_data, indent=4)
logger.info("JSON data pretty-printed")
response = app.response_class(
response=pretty_json_data,
status=200,
mimetype='application/json'
)
return response
except Exception as e:
logger.error(f"Error processing file: {str(e)}")
return jsonify({"error": str(e)}), 500

@app.route("/json-to-excel", methods=["POST"])
def json_to_excel():
logger.info("json_to_excel endpoint accessed")
if not request.json:
logger.error("No JSON provided in request")
return "Please provide the json", 400
return jsonify({"error": "No JSON provided"}), 400
json_data = request.json
logger.info(f"Processing JSON data: {json_data}")
json_validator.validate_json(json_data)
logger.info("JSON data validated")
df = convert_json_to_excel(json_data)
logger.info("JSON data converted to Excel")
output = BytesIO()
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
df.to_excel(writer, index=False)
output.seek(0)
logger.info("Excel file created and saved to buffer")
return send_file(
output,
as_attachment=True,
download_name="output.xlsx",
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)
try:
logger.info(f"Processing JSON data: {json_data}")
json_validator.validate_json(json_data)
logger.info("JSON data validated")
df = convert_json_to_excel(json_data)
logger.info("JSON data converted to Excel")
output = BytesIO()
with pd.ExcelWriter(output, engine="xlsxwriter") as writer:
df.to_excel(writer, index=False)
output.seek(0)
logger.info("Excel file created and saved to buffer")
return send_file(
output,
as_attachment=True,
download_name="output.xlsx",
mimetype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)
except Exception as e:
logger.error(f"Error processing JSON: {str(e)}")
return jsonify({"error": str(e)}), 500

@app.route("/validate-json", methods=["POST"])
def validate_json():
logger.info("validate_json endpoint accessed")
json_data = request.json
if not request.json:
logger.error("No JSON provided in request")
return "Please provide the json", 400
return jsonify({"error": "No JSON provided"}), 400
json_data = request.json
try:
json_validator.validate_json(json_data)
logger.info("JSON data is valid")
Expand All @@ -99,11 +105,11 @@ def validate_excel():
logger.error("No selected file")
return jsonify({"error": "No selected file"}), 400
if file:
logger.info(f"Processing file: {file.filename}")
file_stream = BytesIO(file.read())
df = pd.read_excel(file_stream, engine="openpyxl")
logger.info("Excel file read into DataFrame")
try:
logger.info(f"Processing file: {file.filename}")
file_stream = BytesIO(file.read())
df = pd.read_excel(file_stream, engine="openpyxl")
logger.info("Excel file read into DataFrame")
excel_validator.validate_excel(df)
logger.info("Excel file is valid")
return jsonify({"message": "Data model is valid."})
Expand Down
2 changes: 2 additions & 0 deletions data_quality_tool/frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ <h2>Convert JSON to Excel</h2>
<button onclick="convertJsonToExcel()">Convert to Excel</button>
<a id="downloadExcel" style="display: none;">Download Excel</a>
</div>

<div id="error-message" style="display: none; color: red;"></div>
</div>
<script src="script.js"></script>
</body>
Expand Down
88 changes: 53 additions & 35 deletions data_quality_tool/frontend/script.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,75 @@
function displayError(message) {
const errorMessage = document.getElementById('error-message');
errorMessage.textContent = message;
errorMessage.style.display = 'block';
}

async function convertExcelToJson() {
const excelFile = document.getElementById('excelFile').files[0];
if (!excelFile) {
alert("Please select an Excel file first.");
displayError("Please select an Excel file first.");
return;
}

const formData = new FormData();
formData.append('file', excelFile);

const response = await fetch('http://127.0.0.1:8000/excel-to-json', {
method: 'POST',
body: formData
});

if (response.ok) {
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const downloadLink = document.getElementById('downloadJson');
downloadLink.href = url;
downloadLink.download = 'data.json';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download JSON';
} else {
alert("Failed to convert Excel to JSON.");
try {
const response = await fetch('http://127.0.0.1:8000/excel-to-json', {
method: 'POST',
body: formData
});

if (response.ok) {
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const downloadLink = document.getElementById('downloadJson');
downloadLink.href = url;
downloadLink.download = 'data.json';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download JSON';
displayError(""); // Clear previous error message
} else {
const errorData = await response.json();
displayError(`Failed to convert Excel to JSON: ${errorData.error}`);
}
} catch (error) {
displayError(`An error occurred: ${error.message}`);
}
}

async function convertJsonToExcel() {
const jsonFile = document.getElementById('jsonFile').files[0];
if (!jsonFile) {
alert("Please select a JSON file first.");
displayError("Please select a JSON file first.");
return;
}

const fileContent = await jsonFile.text();

const response = await fetch('http://127.0.0.1:8000/json-to-excel', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: fileContent
});

if (response.ok) {
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const downloadLink = document.getElementById('downloadExcel');
downloadLink.href = url;
downloadLink.download = 'data.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel';
} else {
alert("Failed to convert JSON to Excel.");
try {
const response = await fetch('http://127.0.0.1:8000/json-to-excel', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: fileContent
});

if (response.ok) {
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const downloadLink = document.getElementById('downloadExcel');
downloadLink.href = url;
downloadLink.download = 'data.xlsx';
downloadLink.style.display = 'block';
downloadLink.textContent = 'Download Excel';
displayError(""); // Clear previous error message
} else {
const errorData = await response.json();
displayError(`Failed to convert JSON to Excel: ${errorData.error}`);
}
} catch (error) {
displayError(`An error occurred: ${error.message}`);
}
}
4 changes: 4 additions & 0 deletions data_quality_tool/frontend/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ a {
margin-top: 10px;
color: #007bff;
}

#error-message {
margin-top: 20px;
}

0 comments on commit 40c052c

Please sign in to comment.