Commit 18cb69af authored by W.D.R.P. Sandeepa's avatar W.D.R.P. Sandeepa

Merge branch 'it18218640' into 'master'

add new flask file

See merge request !52
parents c1e60236 30ea6b7d
from flask import Flask, redirect, url_for, render_template, request, jsonify, make_response
from API.routers.router import funtion_one
app = Flask(__name__)
@app.route("/")
def home():
return render_template('home.html')
@app.route("/testings")
def checkothers():
return "testing funtions"
@app.route("/ru")
def abc():
response_val = funtion_one()
d = jsonify(
message="Successfully request done",
data=response_val
)
return make_response(d, 200)
return response_val
if __name__ == "__main__":
app.run(host='192.168.8.101')
#app.run(host='192.168.8.100,port='5000', debug=True)
# app.run(debug=True)
\ No newline at end of file
import mysql.connector
from sqlalchemy import create_engine
import pandas as pd
import json
from API.routers.dic_config import create_dict
def create_con():
db = mysql.connector.connect(
database="helply",
host="127.0.0.1",
user="root",
password="rp19970520"
)
return db
def create_con_pandas():
db_connection_str = 'mysql+mysqlconnector://root:rp19970520@127.0.0.1/helply'
db = create_engine(db_connection_str)
return db
def insert_data_pandas_df(pd_df, table_name):
db = create_con_pandas()
pd_df.to_sql(name=table_name, con=db, if_exists='append', index=False)
def get_data_sql(sql):
db = create_con_pandas()
return pd.read_sql_query(sql=sql, con=db)
def insert_data_json(sql_query):
db = create_con()
cursor = db.cursor()
cursor.execute(sql_query)
db.commit()
db.close()
return "INFO: insert successful"
def getDatas(query):
data_dic = create_dict()
db = create_con()
cursor = db.cursor()
cursor.execute(query)
result = cursor.fetchall()
#idcolorActivities, name, image
for row in result:
data_dic.add(row[0], ({"idcolorActivities": row[0], "name": row[1], "image": row[2]}))
stud_json = json.dumps(data_dic, indent=1, sort_keys=True)
print(f"{stud_json}")
return stud_json
def insert_data(sql_query):
db = create_con()
cursor = db.cursor()
try:
cursor.execute(sql_query)
db.commit()
db.close()
return 1
except Exception as e:
db.rollback()
db.close()
print(e)
return e
def insert_data_get_id(sql_query):
db = create_con()
cursor = db.cursor()
try:
cursor.execute(sql_query)
id = cursor.lastrowid
db.commit()
db.close()
return 1, id
except Exception as e:
db.rollback()
db.close()
print(e)
return e
def insert_data_main_response(trip_info_sql, trip_df):
db = create_con()
cursor = db.cursor()
try:
cursor.execute(trip_info_sql)
insert_data_pandas_df(trip_df, 'trip')
db.commit()
db.close()
return 1
except Exception as e:
db.rollback()
db.close()
return e
def get_data(sql_query):
db = create_con()
cursor = db.cursor()
cursor.execute(sql_query)
results = cursor.fetchall()
db.close()
return results
def get_data_list(sql_query):
db = create_con()
cursor = db.cursor()
cursor.execute(sql_query)
results = list(cursor.fetchall())
db.close()
return results
def update_data(sql_query):
db = create_con()
cursor = db.cursor()
try:
cursor.execute(sql_query)
db.commit()
db.close()
return 1
except Exception as e:
db.rollback()
db.close()
print(e)
return e
class create_dict(dict):
# __init__ function
def __init__(self):
self = dict()
# Function to add key:value
def add(self, key, value):
self[key] = value
#calling one router file
from API.db.dbConnection import get_data_sql, getDatas
def funtion_one():
qry = 'SELECT * FROM coloractivities' # employee
# name = get_data_sql(qry).to_json()
name = getDatas(qry)
# name = "working"
return name
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Sample Home</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
</head>
<body>
<h1>Welcome to home of python</h1>
</body>
</html>
\ 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