Data not getting saved in Flask DB
I am writing this POST request endpoint in Flask:
​
​
​
​
​
​
​
The problem is when I run the application, there is another GET request that fetches the records that should have been saved as part of this POST request in the DB, while in reality, I see no records being saved in the database. Can someone help me to know what I might be missing here?
/r/flask
https://redd.it/1anugqh
I am writing this POST request endpoint in Flask:
​
app.route('/transactions', methods=['POST'])def upload_transactions():file = request.files['data']if 'data' not in request.files:return 'No file part', 400if file.filename == '':return 'No selected file', 400if file:#define headersheaders = ['Date', 'Type', 'Amount($)', 'Memo']​
# read csv datacsv_data = StringIO(`file.stream.read`().decode("UTF8"), newline=None)​
# add headers to the beginning of the input csv filecsv_content = ','.join(headers) + '\n' + csv_data.getvalue()​
#reset file position to the beginningcsv_data.seek(0)​
#Read csv file with headers nowtransactions = csv.reader(csv_data)​
for row in transactions:if len(row) != 4:return 'Invalid CSV format', 400try:date = datetime.datetime.strptime(row[0], "%m/%d/%Y").date()type = row[1]amount = float(row[2])memo = row[3]transaction = Transaction(date=date, type=type, amount=amount, memo=memo)db.session.add(transaction)except ValueError:db.session.rollback()return 'Invalid amount format', 400db.session.commit()return 'Transactions uploaded successfully', 201​
The problem is when I run the application, there is another GET request that fetches the records that should have been saved as part of this POST request in the DB, while in reality, I see no records being saved in the database. Can someone help me to know what I might be missing here?
/r/flask
https://redd.it/1anugqh
What could I have done better here?
Hi, I'm pretty new to Python, and actual scripting in general, and I just wanted to ask if I could have done anything better here. Any critiques?
import time
import colorama
from colorama import Fore, Style
color = 'WHITE'
colorvar2 = 'WHITE'
#Reset colors
print(Style.RESETALL)
#Get current directory (for debugging)
#print(os.getcwd())
#Startup message
print("Welcome to the ASCII art reader. Please set the path to your ASCII art below.")
#Bold text
print(Style.BRIGHT)
#User-defined file path
path = input(Fore.BLUE + 'Please input the file path to your ASCII art: ')
color = input('Please input your desired color (default: white): ' + Style.RESETALL)
#If no ASCII art path specified
if not path:
print(Fore.RED +
/r/Python
https://redd.it/1p4ffmh
Hi, I'm pretty new to Python, and actual scripting in general, and I just wanted to ask if I could have done anything better here. Any critiques?
import time
import colorama
from colorama import Fore, Style
color = 'WHITE'
colorvar2 = 'WHITE'
#Reset colors
print(Style.RESETALL)
#Get current directory (for debugging)
#print(os.getcwd())
#Startup message
print("Welcome to the ASCII art reader. Please set the path to your ASCII art below.")
#Bold text
print(Style.BRIGHT)
#User-defined file path
path = input(Fore.BLUE + 'Please input the file path to your ASCII art: ')
color = input('Please input your desired color (default: white): ' + Style.RESETALL)
#If no ASCII art path specified
if not path:
print(Fore.RED +
/r/Python
https://redd.it/1p4ffmh
Reddit
From the Python community on Reddit
Explore this post and more from the Python community