Commit 43d4dbb0 by arjun.b

fast api

parents
# Default ignored files
/shelf/
/workspace.xml
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N801" />
</list>
</option>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/new_pymongo.iml" filepath="$PROJECT_DIR$/.idea/new_pymongo.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Python 3.9" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
import uvicorn
from fastapi import FastAPI
from scripts.constants.db_connection import db_connect
from scripts.utils.mongo_utils import create_collection
if __name__ == "__main__":
try:
uvicorn.run("scripts.services.main:app", port=8000, reload=True)
except Exception as e:
print(str(e))
from pymongo import MongoClient
def db_connect():
try:
conn = MongoClient("mongodb://localhost:27017")
database = conn.employees
print(conn.list_database_names())
dblist = conn.list_database_names()
if database in dblist:
print("database exist")
collection = database.employee
print("mongo connect successfully")
return database
except Exception as e:
print(str(e))
from mongoengine import Document, StringField, ObjectIdField, IntField
class Employee(Document):
_id: ObjectIdField()
id: IntField()
first_name: StringField()
last_name: StringField()
gender: StringField()
phone: StringField()
def read_all(collection):
response=collection.find({})
\ No newline at end of file
def insert_data(collection, data):
inserted = collection.insert_many(data.to_dict('records'))
if inserted:
return True
else:
return False
import pandas as pd
def read_file():
try:
dataset=pd.read_csv(r'E:\new_pymongo\temp\emp.csv')
return dataset
except Exception as e:
print(str(e))
\ No newline at end of file
from fastapi import FastAPI
from scripts.constants.db_connection import db_connect
from scripts.core.handlers.find_all import read_all
from scripts.core.handlers.insert_data import insert_data
from scripts.core.handlers.read_file import read_file
from scripts.utils.mongo_utils import create_collection
from scripts.core.engine.model import Employee
from mongoengine import connect
app = FastAPI()
connect(db="employees", host="localhost", port=27017)
@app.get("/")
async def root():
return {"data": "hello"}
@app.get("/get_details", tags=["read data"])
def get_all_details():
employees = Employee.objects().to_json()
# print(employees)
return {"data": employees}
# read csv file
data = read_file()
# create database
db = db_connect()
# create collection
collection = create_collection(db)
# insert the csv file into database
insert = insert_data(collection, data)
print("data inserted from csv")
read = read_all(collection)
def create_collection(db):
collection_name="employee"
collection=db[collection_name]
return collection
This diff is collapsed. Click to expand it.
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