how to display data from MySql db to html pages in python - html

i want to display all the data in my table to a webpage using python when i click a button. here is my app.py code can anyone help me to finish this please..
def POST(self):
form = web.input(name="a", newname="s", number="d")
conn = MySQLdb.connect(host= "localhost", user="root", passwd="", db="testdb")
x = conn.cursor()
x.execute("SELECT * FROM details")
conn.commit()
items = cursor.fetchall()
return items()
conn.rollback()
conn.close()
return render.index()
i want to display it on index.html.and table has 3 colums

def POST(self):
form = web.input(name="a", newname="s", number="d")
conn = MySQLdb.connect(host= "localhost", user="root", passwd="", db="testdb")
x = conn.cursor()
x.execute("SELECT * FROM details")
conn.commit()
items = cursor.fetchall()
for row in items:
print row[0], row[1]
conn.rollback()
conn.close()
return render.index(items)
Run this code and check output in terminal and let me know the data is comming or not

Related

FastAPI not running all the functions to return the right values from database

I am trying to make a twitter points program. Basically, you get points based off of the number of likes, retweets and replies your post with a specified hashtag gets. I made an API to get these points from a database but fastAPI is not doing all the funtions specified to return the correct values.
API code:
DATABASE_URL = "mysql+mysqlconnector://root:password#localhost:3306/twitterdb"
database = Database(DATABASE_URL)
metadata_obj = MetaData()
engine = create_engine(
DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
metadata = sqlalchemy.MetaData()
Base = declarative_base()
user_points = sqlalchemy.Table(
"points",
metadata_obj,
sqlalchemy.Column("username", sqlalchemy.String,),
sqlalchemy.Column("rt_points", sqlalchemy.Integer,),
sqlalchemy.Column("reply_points", sqlalchemy.Integer),
sqlalchemy.Column("like_points", sqlalchemy.Integer),
sqlalchemy.Column("total_points", sqlalchemy.Integer)
)
engine = sqlalchemy.create_engine(
DATABASE_URL
)
metadata.create_all(engine)
app = FastAPI()
#app.on_event("startup")
async def connect():
await database.connect()
#app.on_event("shutdown")
async def shutdown():
await database.disconnect()
class UserName(BaseModel):
rt_points: int
reply_points: int
like_points: int
total_points : int
#app.get('/userdata/', response_model=UserName)
async def get_points(user: str):
username=user
metrics.clear()
tweets_list = tweet_id(username)
tweets_list.get_tweet_ids(str(username))
metrics.main()
summing=summer(username)
summing.sum_fun(str(username))
query = user_points.select().where(user_points.c.username == username)
user = await database.fetch_one(query)
return {**user}
if __name__ == "__main__":
uvicorn.run("main:app", reload= True, host="127.0.0.1", port=5000, log_level="info")
code for metrics.py:
ids=[]
class tweet_id:
def __init__(self, name):
self.name = name
def get_tweet_ids(self, name):
try:
connection = mysql.connector.connect(host='localhost',
database='twitterdb',
user='root',
password='password')
cursor = connection.cursor()
query="truncate table twitterdb.points"
query1="truncate table twitterdb.Metrics"
sql_select_query = """SELECT tweetid FROM twitterdb.StreamData WHERE username = %s"""
# set variable in query
cursor.execute(query)
cursor.execute(query1)
cursor.execute(sql_select_query, (name,))
# fetch result
record = cursor.fetchall()
for row in record:
ids.append(int(row[0]))
except mysql.connector.Error as error:
print("Failed to get record from MySQL table: {}".format(error))
finally:
if connection.is_connected():
cursor.close()
connection.close()
def create_url():
tweet_fields = "tweet.fields=public_metrics"
converted_list = [str(element) for element in ids]
id_list = ",".join(converted_list)
url = "https://api.twitter.com/2/tweets?ids={}&{}".format(id_list, tweet_fields)
return url
#curl 'https://api.twitter.com/2/tweets?ids=1459764778088337413&tweet.fields=public_metrics&expansions=attachments.media_keys&media.fields=public_metrics' --header 'Authorization: Bearer $Bearer
def bearer_oauth(r):
"""
Method required by bearer token authentication.
"""
r.headers["Authorization"] = f"Bearer {bearer_token}"
return r
def connect_to_endpoint(url):
response = requests.request("GET", url, auth=bearer_oauth)
print(response.status_code)
if response.status_code != 200:
raise Exception(
"Request returned an error: {} {} {}".format(
response.status_code, response.text, ids
)
)
return url
return response.json()
def main():
def append_to_database(json_response):
#Loop through each tweet
for tweet in json_response['data']:
# Tweet ID
tweetid = tweet['id']
# Tweet metrics
retweet_count = tweet['public_metrics']['retweet_count']
reply_count = tweet['public_metrics']['reply_count']
like_count = tweet['public_metrics']['like_count']
quote_count = tweet['public_metrics']['quote_count']
connect(tweetid, retweet_count, reply_count, like_count, quote_count)
def connect(tweetid, retweet_count, reply_count, like_count, quote_count):
"""
connect to MySQL database and insert twitter data
"""
try:
con = mysql.connector.connect(host='localhost',
database='twitterdb', user='root', password='passsword', charset='utf8')
if con.is_connected():
"""
Insert twitter data
"""
cursor = con.cursor(buffered=True)
# twitter, golf
delete_previous_data_query = "truncate table Metrics"
query = "INSERT INTO Metrics (tweetid,retweet_count,reply_count,like_count,quote_count) VALUES (%s, %s, %s, %s, %s)"
cursor.execute(delete_previous_data_query)
cursor.execute(query, (tweetid,retweet_count,reply_count,like_count,quote_count))
con.commit()
except Error as e:
print(e)
cursor.close()
con.close()
return
url = create_url()
json_response = connect_to_endpoint(url)
append_to_database(json_response)
#Function to calculate sum of points and display it
class summer:
def __init__(self, name):
self.name = name
def sum_fun(self, name):
try:
con = mysql.connector.connect(host='localhost',
database='twitterdb', user='root', password='password', charset='utf8')
if con.is_connected():
cursor = con.cursor(buffered=True)
def create_points_table():
query= ("INSERT INTO twitterdb.points(username, rt_points,reply_points,like_points,total_points) (SELECT %s, SUM(quote_count + retweet_count) * 150, SUM(reply_count) * 50, SUM(like_count) * 10, SUM(quote_count + retweet_count) * 150 + SUM(reply_count) * 50 + SUM(like_count) * 10 FROM twitterdb.Metrics)")
cursor.execute(query, (name,))
con.commit()
create_points_table();
except Error as e:
print(e)
cursor.close()
con.close()
def clear():
"""
connect to MySQL database and insert twitter data
"""
try:
con = mysql.connector.connect(host='localhost',
database='twitterdb', user='root', password='password', charset='utf8')
if con.is_connected():
cursor = con.cursor(buffered=True)
clear_points = ("truncate table twitterdb.points")
cursor.execute(clear_points)
except Error as e:
print(e)
cursor.close()
con.close()
return
What happens here is that there's a database named twitterdb with the tables StreamData, metrics, and points.
StreamData containts tweetids and usernames of the posts that were tweeted with the specified hashtag and it is build with the Streaming API.
Here the issues is that, suppose I have the following usernames mark and ramon in the streamdata table. So when I input the username via the API as mark no issues happen, it returns the correct points for mark, but if I then enter something like mark1 or any random value, it returns the points for mark again. But then if I enter ramon it gives the right points for ramon but then if I enter the random values again, I get the same points for ramon.
Furthermore, the first time when we start the API and if we enter a random value, it returns an error that is specified in the exception as defined in connect_to_endpoint function.
The code logic here is that,
We enter a username via the API, and the get_tweet_ids function looks for that username in the streamdata table and selects all the tweet ids corresponding to that username and saves it to a list, ids. This list of ids is given to the twitter metrics API endpoint and the required values from the response is saved to the table metrics.
Then, the sum_fun is called to select the sum of values of likes, rts and replies from the metrics table, multiply it with the specified points and save it to the table points along with the username.
The API at last returns the values in the table points matching the username.
How can I get it to stop returning the values for random data? If an invalid data is given, it must raise the exception in connect_to_endpoint function, but it just returns whatever value is in the table points previously.
I tried multiple approaches to this like, clearing the values of points before all other functions and checking to return only the values corresponding to the username in the points table. But neither of them worked. When the username was checked in the points table after running it with random values, it contained the random value but with the points of the previous valid username.
NOTE: The table points is a temporary table and values are assigned only when an API call is made.
I am a complete beginner to all this and this is more of a pet project I have been working on, so please help out. Any and all help and guidance regarding my logic and design and a fix for this will be of much use. Thanks.
if the code that you have provided for metrics.py is correct your problem should comme from how you declare the variable ids.
in your code you have declare it as a global so it will not be reset at every function call or class instance creation.
what you should to is declare it in get_tweet_ids()
class tweet_id:
def __init__(self, name):
self.name = name
def get_tweet_ids(self, name):
ids=[] # modification here
try:
connection = mysql.connector.connect(host='localhost',
database='twitterdb',
user='root',
password='password')
cursor = connection.cursor()
query="truncate table twitterdb.points"
query1="truncate table twitterdb.Metrics"
sql_select_query = """SELECT tweetid FROM twitterdb.StreamData WHERE username = %s"""
# set variable in query
cursor.execute(query)
cursor.execute(query1)
cursor.execute(sql_select_query, (name,))
# fetch result
record = cursor.fetchall()
for row in record:
ids.append(int(row[0]))
return ids # modification here
except mysql.connector.Error as error:
print("Failed to get record from MySQL table: {}".format(error))
finally:
if connection.is_connected():
cursor.close()
connection.close()
with this you will have a new instance of ids at every get_tweet_ids call.
You will have to change the rest of your code according to this return statement

Why isn't fastAPI making the database and returning the result here?

Long code ahead, kindly help out.
I am trying to create a point system for tweets. I have streamed tweets with #Python to a MySQL database and I am trying to create a points system for the same.
from typing_extensions import Self
import requests
import os
import json
import mysql.connector
from mysql.connector import Error
bearer_token = "$Bearer"#Getting tweet ids of specified user from database
ids=[]
class tweet_id:
def __init__(self, name):
self.name = name
def get_tweet_ids(self, name):
try:
connection = mysql.connector.connect(host='localhost',
database='twitterdb',
user='root',
password='pasword#123')
cursor = connection.cursor()
sql_select_query = """SELECT tweetid FROM twitterdb.StreamData WHERE username = %s"""
# set variable in query
cursor.execute(sql_select_query, (name,))
# fetch result
record = cursor.fetchall()
for row in record:
ids.append(int(row[0]))
except mysql.connector.Error as error:
print("Failed to get record from MySQL table: {}".format(error))
"""finally:
if connection.is_connected():
cursor.close()
connection.close()"""
def create_url():
tweet_fields = "tweet.fields=public_metrics"
converted_list = [str(element) for element in ids]
id_list = ",".join(converted_list)
url = "https://api.twitter.com/2/tweets?ids={}&{}".format(id_list, tweet_fields)
return url
def bearer_oauth(r):
"""
Method required by bearer token authentication.
"""
r.headers["Authorization"] = f"Bearer {bearer_token}"
return r
def connect_to_endpoint(url):
response = requests.request("GET", url, auth=bearer_oauth)
print(response.status_code)
if response.status_code != 200:
raise Exception(
"Request returned an error: {} {} {}".format(
response.status_code, response.text, ids
)
)
return url
return response.json()
def main():
#def __init__(connect, append_to_database):
#Self.connect = connect
#Self.append_to_database = append_to_database
def connect(tweetid, retweet_count, reply_count, like_count, quote_count):
"""
connect to MySQL database and insert twitter data
"""
try:
con = mysql.connector.connect(host='localhost',
database='twitterdb', user='root', password='pasword#123', charset='utf8')
if con.is_connected():
"""
Insert twitter data
"""
cursor = con.cursor(buffered=True)
# twitter, golf
query = "INSERT INTO Metrics (tweetid,retweet_count,reply_count,like_count,quote_count) VALUES (%s, %s, %s, %s, %s)"
cursor.execute(query, (tweetid,retweet_count,reply_count,like_count,quote_count))
con.commit()
except Error as e:
print(e)
cursor.close()
con.close()
return
def append_to_database(json_response):
#Loop through each tweet
for tweet in json_response['data']:
# Tweet ID
tweetid = tweet['id']
# Tweet metrics
retweet_count = tweet['public_metrics']['retweet_count']
reply_count = tweet['public_metrics']['reply_count']
like_count = tweet['public_metrics']['like_count']
quote_count = tweet['public_metrics']['quote_count']
connect(tweetid, retweet_count, reply_count, like_count, quote_count)
url = create_url()
json_response = connect_to_endpoint(url)
append_to_database(json_response)
#function for connecting and inserting to database
#Function to calculate sum of points and display it
class summer:
like_points=0
reply_points=0
total_rts=0
rt_points=0
total=0
def sum_fun():
try:
con = mysql.connector.connect(host='localhost',
database='twitterdb', user='root', password='pasword#123', charset='utf8')
if con.is_connected():
cursor = con.cursor(buffered=True)
def sum_rts():
cursor.execute("SELECT SUM(retweet_count) FROM twitterdb.Metrics")
sum1=cursor.fetchall()[0][0]
if sum1 is None:
return 0;
else:
return int(sum1)
def sum_replies():
cursor.execute("SELECT SUM(reply_count) FROM twitterdb.Metrics")
sum2=cursor.fetchall()[0][0]
if sum2 is None:
return 0
else:
return int(sum2)
def sum_likes():
cursor.execute("SELECT SUM(like_count) FROM twitterdb.Metrics")
sum3=cursor.fetchall()[0][0]
if sum3 is None:
return 0
else:
return int(sum3)
def sum_qts():
cursor.execute("SELECT SUM(quote_count) FROM twitterdb.Metrics")
sum4=cursor.fetchall()[0][0]
if sum4 is None:
return 0
else:
return int(sum4)
like_points= (20*(sum_likes()))
reply_points= (100 * (sum_replies()))
total_rts= (sum_rts() + sum_qts())
rt_points = (300 * total_rts)
total = (like_points + reply_points + rt_points)
return total
#print("Like Points:", like_points)
#print("Reply Points:", reply_points)
#print("Retweet Points:", rt_points)
#print("Total Points:",total)
# print(points)
except Error as e:
print(e)
cursor.close()
con.close()
def clear():
"""
connect to MySQL database and insert twitter data
"""
try:
con = mysql.connector.connect(host='localhost',
database='twitterdb', user='root', password='Mysql#123', charset='utf8')
if con.is_connected():
cursor = con.cursor(buffered=True)
cursor.execute("truncate table twitterdb.Metrics")
except Error as e:
print(e)
#cursor.close()
#con.close()
return
Furthermore I have created an API on FastAPI to trigger all the functionalities in the above script and get the outputs,like_points, reply_points,rt_points and total sent via an API.The API accepts the value username via a POST request and triggers the script.
API code:
from fastapi import FastAPI
from pydantic import BaseModel
from metrics import tweet_id
from metrics import create_urls
from metrics import summer
import metrics
import uvicorn
from typing_extensions import Self
app = FastAPI()
class Username(BaseModel):
username:str
#app.post('/Username')
def Username(Username : Username):
username=Username.username
tweets_list = tweet_id(username)
tweets_list.get_tweet_ids(str(username))
metrics.clear()
metrics.main()
points=summer.sum_fun()
return{points.total}
if __name__ == "__main__":
uvicorn.run("api:app", host="127.0.0.1", port=5000, log_level="info")
I am unable to get the output and even though the request is completed I get null as the result. Why is that happening? Also, I am very new to a lot of this so code improvement suggestions and modifications are very welcome. Thank you.
you have commented your return in sum_fun() function
total = (like_points + reply_points + rt_points)
#return total
#print("Like Points:", like_points)
that's the reason None is returned when sum_fun() is invocated.

PYTHON SQLITE3 DELETE FROM LIST

I want to delete data from selected in ListBox. I tried everything but it's not working. I tried in simple file. It worked. I wrote same code here but it's not working. I'm stuck since two days. Please help.
from tkinter import *
import sqlite3
from tkinter import messagebox
import randevuEkleme as fRE
from tkcalendar import *
def randevularFormunuAc():
root = Tk()
root.title("Randevu Listesi Formu")
root.geometry("750x500")
randevuListesi = Listbox(root, width=50, bg="yellow", selectmode=SINGLE)
takvim = Calendar(root, selectmode="day", year=2020, month=5, day=22)
takvim.grid(row=0, column=0)
randevuListesi.grid(row=1, column=0)
secilen = randevuListesi.curselection()
def randevulariListele(): #OK!
con = sqlite3.connect('veri2.db')
c = con.cursor()
c.execute("SELECT * FROM randevular")
liste = c.fetchall()
con.commit()
con.close()
randevuListesi.delete(first=0,last=END)
for i in liste:
randevuListesi.insert(END,i)
con = sqlite3.connect('veri2.db')
c = con.cursor()
c.execute("SELECT * FROM randevular")
liste = c.fetchall()
con.commit()
con.close()
for i in liste:
randevuListesi.insert(END, i)
def randevuSil():
con = sqlite3.connect('veri2.db')
c = con.cursor()
# Problem is HERE!!!
c.execute("DELETE FROM randevular WHERE rowid = (?)", (secilen[0] + 1,))
# PROBLEMIS HERE!!!
con.commit()
con.close()
btnListele = Button(root, text="Randevuları Listele",command=randevulariListele)
btnRandevuEkle = Button(root,text="Randevu Ekle",command=fRE.randevuEklemeFormunuAc)
btnRandevuSil = Button(root,text="Randevuyu Sil",command=randevuSil()) !!!!
btnCikis = Button(root, text="Geri", command=root.quit)
btnListele.grid(row=2,column=0)
btnRandevuEkle.grid(row=2,column=1)
btnRandevuSil.grid(row=2,column=2)
btnCikis.grid(row=2,column=3)
root.mainloop()
and error message is :
File "C:\Users\kutla\PycharmProjects\fizyoOta1.0\randevularFormu.py", line 43, in randevuSil
c.execute("DELETE FROM randevular WHERE rowid = (?)", (secilen[0] + 1,))
IndexError: tuple index out of range
Two possibilities on a quick read:
The line secilen = randevuListesi.curselection() probably needs to be in randevuSil() rather than as part of the setup.
When setting up the button, the () should be omitted in the command=:
btnRandevuSil = Button(root,text="Randevuyu Sil",command=randevuSil)
Otherwise the function will be called as part of setup (when nothing has been selected yet) rather than when the button is clicked.

I retrieved data from MySQL database using python but attribute names are not displayed. How can i display data in python dictionary format?

#!/usr/bin/python
print('Content-type: text/html\r\n\r')
import mysql.connector
import json
conn = mysql.connector.connect(
user='root',
password='',
host='localhost',
database='id1914180_myproducts')
cur = conn.cursor()
query = ("SELECT * FROM Reviews")
cur.execute(query)
result=cur.fetchall()
print(json.dumps(result))
cur.close()
conn.close()
I want the data to be displayed in dictionary format like given:
{'UserName':'Rozi','You Me and Dupree':2.5,'Lady in the water':4.0}

Question marks while fetching data from phpmyadmin in Python 3

Hello I have win 10 Greece. I want to fetch same data (the path of a file) from mysql. The problem is while I retrieve the path instead of the name of the folder I came across with??????. A sample of the code is the following
import pymysql as MySQLdb
dbid = "client"
password = "pass"
database = "clients"
serverip = "192.168.168.150"
db = None
cur = None
try:
db = MySQLdb.connect(host=serverip, user=dbid, passwd=password, db=database, connect_timeout=20)
cur = db.cursor()
except MySQLdb.Error as e:
print("Cannot connect with the Data Base : "), e
query1 = "SELECT path FROM `instructions` WHERE clientmac ='%s'" % (str(5555))
try:
row = None
cur.execute(query1)
db.commit()
row = cur.fetchall()
except MySQLdb.Error as e:
print("Problem while Executing query :", query1, "error : ", e)
print(row)
db.close()
cur.close()
Python 3 uses Unicode so I thing that the problem is not from python but from phpmyadmin. The field path is “utf8_general_ci”. Everything looks fine but why instead of
'C:\\Users\\Γιαννης\\Documents\\Arduino' I am getting 'C:\\Users\\???????\\Documents\\Arduino'
I found the answer. I hat to but some additional coding before the execution of the query. I post the code below.
import pymysql as MySQLdb
dbid = "client"
password = "pass"
database = "clients"
serverip = "192.168.168.150"
socketip = "192.168.168.18"
db = None
cur = None
try:
db = MySQLdb.connect(host=serverip, user=dbid, passwd=password, db=database, use_unicode=True, connect_timeout=20)
cur = db.cursor()
except MySQLdb.Error as e:
print("Cannot connect with the Data Base : "), e
query1 = "SELECT path FROM `instructions` WHERE clientmac ='%s'" % (str(5555))
try:
row = None
db.set_charset('utf8')
cur.execute('SET NAMES utf8;')
cur.execute('SET CHARACTER SET utf8;')
cur.execute('SET character_set_connection=utf8;')
cur.execute(query1)
db.commit()
row = cur.fetchall()
except MySQLdb.Error as e:
print("Problem while Executing query :", query1, "error : ", e)
print(row)
db.close()
cur.close()