Python Daily
2.57K subscribers
1.48K photos
53 videos
2 files
38.9K links
Daily Python News
Question, Tips and Tricks, Best Practices on Python Programming Language
Find more reddit channels over at @r_channels
Download Telegram
Data not getting saved in Flask DB

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', 400

if file.filename == '':

return 'No selected file', 400

if file:

#define headers

headers = ['Date', 'Type', 'Amount($)', 'Memo']

​

# read csv data

csv_data = StringIO(`file.stream.read`().decode("UTF8"), newline=None)

​

# add headers to the beginning of the input csv file

csv_content = ','.join(headers) + '\n' + csv_data.getvalue()

​

#reset file position to the beginning

csv_data.seek(0)

​

#Read csv file with headers now

transactions = csv.reader(csv_data)

​

for row in transactions:

if len(row) != 4:

return 'Invalid CSV format', 400

try:

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', 400

db.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.RESET
ALL)

#If no ASCII art path specified
if not path:
print(Fore.RED +

/r/Python
https://redd.it/1p4ffmh