Commit 59b97928 by arun.uday

commit2

parent 1e12fccd
......@@ -2,7 +2,7 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/task7.iml" filepath="$PROJECT_DIR$/.idea/task7.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/task7_mqtt_receiver.iml" filepath="$PROJECT_DIR$/.idea/task7_mqtt_receiver.iml" />
</modules>
</component>
</project>
\ No newline at end of file
......@@ -5,3 +5,5 @@ import uvicorn
if __name__ == "__main__":
print("Fast API task")
uvicorn.run("scripts.services.app_services_run:app", port=8000)
# pip install python-multipart
......@@ -3,5 +3,5 @@ base_path = scripts/
sub_path = temp/
[file]
file_name_csv = MOCK_DATA.csv
file_name_csv = data.csv
file_name_json = data.json
# dictionary to json
import pandas as pd
import json
# extracting the csv to dictionary
def extract_data(csv_file):
data_csv = pd.read_csv(csv_file)
json_data = data_csv.to_dict(orient="records")
......
<!DOCTYPE HTML>
<html lang="en">
<title>File Upload</title>
<body>
<!-- form upload -->
<form enctype = "multipart/form-data" action = "/submit_file/" method = "post">
<p>Upload File: <input type = "file" name = "file_data" /></p>
<p><input type = "submit" value = "Upload" /></p>
</form>
</body>
</html>
# file upload to csv
def extract_uploaded_data(full_path_csv, file_data):
with open(full_path_csv, "wb") as file_object:
file_object.write(file_data.file.read())
from fastapi import FastAPI
from fastapi import FastAPI, Request, UploadFile, File
import json
from scripts.config.application_config import full_path_csv, full_path_json
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from scripts.config.application_config import full_path_csv
from scripts.core.handlers.dict_to_json import extract_data
from scripts.core.handlers.remove_list_json import remove_list
from paho.mqtt.client import Client
from scripts.core.handlers.uploaded_file_ import extract_uploaded_data
app = FastAPI()
templates = Jinja2Templates("scripts/core/handlers/")
# html form access using api
@app.get("/upload/", response_class=HTMLResponse)
def template_render(request: Request):
return templates.TemplateResponse("upload_file.html", {"request": request})
# handling the form submit using api
@app.post("/submit_file/")
async def submit_file_data(file_data: UploadFile = File(...)):
try:
extract_uploaded_data(full_path_csv, file_data)
except Exception as e:
print(e)
else:
return {"msg": "file uploading successful..."}
@app.get("/")
# reading csv and loading the data to json finally publishing it to mqtt
@app.get("/csv")
def dict_to_json():
try:
dict_data = extract_data(full_path_csv)
list_removed = remove_list(full_path_json, dict_data)
list_removed = remove_list(dict_data)
json_data = json.dumps(list_removed)
client = Client()
client.connect('192.168.0.220', 1883)
......@@ -23,8 +46,4 @@ def dict_to_json():
print(e)
else:
print("Data published")
return {"msg": "Data published"}
id,first_name,last_name
1,Marna,Pergens
2,Morie,Iddens
3,Ilysa,Thonger
4,Catherine,Dundendale
5,Randene,Brookz
6,Hester,Yeude
7,Philomena,Dyott
8,Sarene,Petren
9,Rosella,Kinavan
10,El,Creamer
[
{
"id": 1,
"first_name": "Marna",
"last_name": "Pergens"
},
{
"id": 2,
"first_name": "Morie",
"last_name": "Iddens"
},
{
"id": 3,
"first_name": "Ilysa",
"last_name": "Thonger"
},
{
"id": 4,
"first_name": "Catherine",
"last_name": "Dundendale"
},
{
"id": 5,
"first_name": "Randene",
"last_name": "Brookz"
},
{
"id": 6,
"first_name": "Hester",
"last_name": "Yeude"
},
{
"id": 7,
"first_name": "Philomena",
"last_name": "Dyott"
},
{
"id": 8,
"first_name": "Sarene",
"last_name": "Petren"
},
{
"id": 9,
"first_name": "Rosella",
"last_name": "Kinavan"
},
{
"id": 10,
"first_name": "El",
"last_name": "Creamer"
}
]
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment