From 2bff1c5f0b7ef3faa8c0cf24ce5ae282dd1c8760 Mon Sep 17 00:00:00 2001 From: sifaoufatai Date: Mon, 16 Jun 2025 14:22:53 +0200 Subject: [PATCH 1/6] adding GUI --- requirements.txt | 3 ++ src/main.py | 53 ++++++++++++++++++++++++ src/templates/upload.html | 87 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 src/main.py create mode 100644 src/templates/upload.html diff --git a/requirements.txt b/requirements.txt index 62db677..1795f1c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,7 @@ pytest~=7.4.4 gitpython PyYAML~=6.0.1 setuptools~=68.2.0 +fastapi +uvicorn +python-multipart diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..6e9bf21 --- /dev/null +++ b/src/main.py @@ -0,0 +1,53 @@ +from fastapi import FastAPI, UploadFile, File, Form, Request +from fastapi.responses import FileResponse, HTMLResponse +from fastapi.templating import Jinja2Templates +import shutil +import os +from template_builder import TemplateBuilder + +app = FastAPI() +templates = Jinja2Templates(directory="src/templates") + +@app.get("/", response_class=HTMLResponse) +async def read_form(request: Request): + # Render the upload form HTML page + return templates.TemplateResponse("upload.html", {"request": request}) + +@app.post("/generate-template/") +async def generate_template( + template_parts_list_file: UploadFile = File(...), + project_name: str = Form(...) +): + working_dir = os.path.dirname(os.path.abspath(__file__)) + + # Save the uploaded CSV file temporarily + input_filename = os.path.join(working_dir, f"temp_{template_parts_list_file.filename}") + with open(input_filename, "wb") as buffer: + shutil.copyfileobj(template_parts_list_file.file, buffer) + + # Define the output JSON filename in the current directory + output_filename = os.path.join(working_dir, f"{project_name}.json") + + try: + original_cwd = os.getcwd() + os.chdir(working_dir) + + # Call the backend TemplateBuilder to generate the JSON file + TemplateBuilder(input_filename, output_filename) + + # Send the generated JSON file back for download + return FileResponse( + path=output_filename, + filename=f"{project_name}.json", + media_type='application/json' + ) + + except Exception as e: + # Return error message in case of failure + return {"error": str(e)} + + finally: + os.chdir(original_cwd) + # Clean up the temporary uploaded CSV file + if os.path.exists(input_filename): + os.remove(input_filename) diff --git a/src/templates/upload.html b/src/templates/upload.html new file mode 100644 index 0000000..d275a29 --- /dev/null +++ b/src/templates/upload.html @@ -0,0 +1,87 @@ + + + + + Générateur de Template + + + +
+

Générateur de Template

+
+ + + + + + + +
+ + + + +
+ + From f8d5ea55c3c423c88b8764423bdefa5f8bc6368d Mon Sep 17 00:00:00 2001 From: sifaoufatai Date: Mon, 16 Jun 2025 14:36:27 +0200 Subject: [PATCH 2/6] adding GUI --- src/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.py b/src/main.py index 6e9bf21..ebedb83 100644 --- a/src/main.py +++ b/src/main.py @@ -7,7 +7,6 @@ app = FastAPI() templates = Jinja2Templates(directory="src/templates") - @app.get("/", response_class=HTMLResponse) async def read_form(request: Request): # Render the upload form HTML page @@ -21,7 +20,8 @@ async def generate_template( working_dir = os.path.dirname(os.path.abspath(__file__)) # Save the uploaded CSV file temporarily - input_filename = os.path.join(working_dir, f"temp_{template_parts_list_file.filename}") + input_filename = os.path.join(working_dir, + f"temp_{template_parts_list_file.filename}") with open(input_filename, "wb") as buffer: shutil.copyfileobj(template_parts_list_file.file, buffer) From 8ef43f08d20ff132ffb480e9a084e1980f91ff94 Mon Sep 17 00:00:00 2001 From: sifaoufatai Date: Mon, 16 Jun 2025 15:01:39 +0200 Subject: [PATCH 3/6] adding GUI --- src/main.py | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/main.py b/src/main.py index ebedb83..ccaceae 100644 --- a/src/main.py +++ b/src/main.py @@ -7,10 +7,14 @@ app = FastAPI() templates = Jinja2Templates(directory="src/templates") + +# Définir la racine pour servir le formulaire HTML @app.get("/", response_class=HTMLResponse) async def read_form(request: Request): - # Render the upload form HTML page - return templates.TemplateResponse("upload.html", {"request": request}) + # Affiche la page HTML du formulaire d'upload + return templates.TemplateResponse( + "upload.html", {"request": request} + ) @app.post("/generate-template/") async def generate_template( @@ -19,35 +23,40 @@ async def generate_template( ): working_dir = os.path.dirname(os.path.abspath(__file__)) - # Save the uploaded CSV file temporarily - input_filename = os.path.join(working_dir, - f"temp_{template_parts_list_file.filename}") + # Sauvegarde temporaire du fichier CSV uploadé + input_filename = os.path.join( + working_dir, f"temp_{template_parts_list_file.filename}" + ) with open(input_filename, "wb") as buffer: - shutil.copyfileobj(template_parts_list_file.file, buffer) + shutil.copyfileobj( + template_parts_list_file.file, buffer + ) - # Define the output JSON filename in the current directory - output_filename = os.path.join(working_dir, f"{project_name}.json") + # Définir le nom du fichier JSON de sortie + output_filename = os.path.join( + working_dir, f"{project_name}.json" + ) try: original_cwd = os.getcwd() os.chdir(working_dir) - # Call the backend TemplateBuilder to generate the JSON file + # Générer le JSON avec TemplateBuilder TemplateBuilder(input_filename, output_filename) - # Send the generated JSON file back for download + # Retourner le fichier JSON généré return FileResponse( path=output_filename, filename=f"{project_name}.json", - media_type='application/json' + media_type="application/json" ) except Exception as e: - # Return error message in case of failure + # Retourner un message d'erreur en cas d'échec return {"error": str(e)} finally: os.chdir(original_cwd) - # Clean up the temporary uploaded CSV file + # Nettoyer le fichier CSV temporaire if os.path.exists(input_filename): - os.remove(input_filename) + os.remove(input_filename) \ No newline at end of file From 43ea01348dc0d7b790119ddc6f77a647a3113782 Mon Sep 17 00:00:00 2001 From: sifaoufatai Date: Mon, 16 Jun 2025 15:07:17 +0200 Subject: [PATCH 4/6] adding GUI --- src/main.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main.py b/src/main.py index ccaceae..4eefea5 100644 --- a/src/main.py +++ b/src/main.py @@ -8,14 +8,15 @@ app = FastAPI() templates = Jinja2Templates(directory="src/templates") -# Définir la racine pour servir le formulaire HTML + @app.get("/", response_class=HTMLResponse) async def read_form(request: Request): - # Affiche la page HTML du formulaire d'upload + # Display the HTML upload form page return templates.TemplateResponse( "upload.html", {"request": request} ) + @app.post("/generate-template/") async def generate_template( template_parts_list_file: UploadFile = File(...), @@ -23,7 +24,7 @@ async def generate_template( ): working_dir = os.path.dirname(os.path.abspath(__file__)) - # Sauvegarde temporaire du fichier CSV uploadé + # Temporarily save the uploaded CSV file input_filename = os.path.join( working_dir, f"temp_{template_parts_list_file.filename}" ) @@ -32,7 +33,7 @@ async def generate_template( template_parts_list_file.file, buffer ) - # Définir le nom du fichier JSON de sortie + # Define the output JSON filename output_filename = os.path.join( working_dir, f"{project_name}.json" ) @@ -41,10 +42,10 @@ async def generate_template( original_cwd = os.getcwd() os.chdir(working_dir) - # Générer le JSON avec TemplateBuilder + # Generate the JSON with TemplateBuilder TemplateBuilder(input_filename, output_filename) - # Retourner le fichier JSON généré + # Return the generated JSON file return FileResponse( path=output_filename, filename=f"{project_name}.json", @@ -52,11 +53,11 @@ async def generate_template( ) except Exception as e: - # Retourner un message d'erreur en cas d'échec + # Return an error message if something fails return {"error": str(e)} finally: os.chdir(original_cwd) - # Nettoyer le fichier CSV temporaire + # Clean up the temporary CSV file if os.path.exists(input_filename): os.remove(input_filename) \ No newline at end of file From e72cae21e00eaca900e528c9a3ed0d4f847ca3f2 Mon Sep 17 00:00:00 2001 From: sifaoufatai Date: Mon, 16 Jun 2025 15:10:40 +0200 Subject: [PATCH 5/6] adding GUI --- src/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index 4eefea5..baf92e8 100644 --- a/src/main.py +++ b/src/main.py @@ -60,4 +60,4 @@ async def generate_template( os.chdir(original_cwd) # Clean up the temporary CSV file if os.path.exists(input_filename): - os.remove(input_filename) \ No newline at end of file + os.remove(input_filename) From d529534e35e79797f4680062616f41336ffc3044 Mon Sep 17 00:00:00 2001 From: sifaoufatai Date: Mon, 16 Jun 2025 23:52:19 +0200 Subject: [PATCH 6/6] adding GUI --- src/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.py b/src/main.py index baf92e8..9a4f558 100644 --- a/src/main.py +++ b/src/main.py @@ -22,8 +22,9 @@ async def generate_template( template_parts_list_file: UploadFile = File(...), project_name: str = Form(...) ): - working_dir = os.path.dirname(os.path.abspath(__file__)) + working_dir = os.path.dirname( + os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # Temporarily save the uploaded CSV file input_filename = os.path.join( working_dir, f"temp_{template_parts_list_file.filename}" @@ -41,7 +42,6 @@ async def generate_template( try: original_cwd = os.getcwd() os.chdir(working_dir) - # Generate the JSON with TemplateBuilder TemplateBuilder(input_filename, output_filename)