/r/flask
Flask is a Python micro-framework for web development. Flask is easy to get started with and a great way to build websites and web applications.
Use [Ask Flask] or [AF] if you have a very specific problem and need help with code.
Use [Extension-name] if you are discussing a certain extension to Flask.
Also check out /r/python or /r/django
/r/flask
i'm trying to config a Gmail account's email server, bit when i run the application, i receive this error:
smtplib.SMTPAuthenticationError: (535, b'5.7.8 Username and Password not accepted. For more information, go to\n5.7.8
https://support.google.com/mail/?p=BadCredentials
5b1f17b1804b1-438e244f38esm124208605e9.26 - gsmtp')
My configuration is:
MAIL_SERVER=smtp.googlemail.com
MAIL_PORT=587
MAIL_USE_TLS=1
MAIL_USERNAME=my.username1234
MAIL_PASSWORD=my google app password (I tried to write it attached but i receive the same error)
I've learned Python fundamentals and frontend. Now I want to start learning Flask for backend. I've tried Miguel Grinberg's course. But I failed to understand.
Can you guys suggest me some other good Flask courses or tutorials that start from beginner topics and cover advanced topics?
I'm creating a flask app following the mega-tutorial of Miguel. I'm trying to send an email to me but i receive this error:
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
How can i resolve it? I think because the client port is different from server port.
I have a Python flask web application that takes the data from a shopify webhook and appends rows to Google sheet. Since it is a webhook, I want it to be running 24/7 as customers can place orders round the clock. I have tested it on my local machine and the code works fine but since then, I have tested it on Render, Railway.app and Pythonanywhere and none of those servers are working with the webhook data or are running 24/7. How can I run the app 24/7 on a cloud server?
The code runs fine on Railway.app and Render and authenticates the OAuth but when the webhooks is tested, it does not generate any response and moreover the app stops running after a while.
I tested the same app on my local machine using ngrok and every time a new order is placed, it does generate the expected results (adds rows to Google sheet).
Been googling about this and I hear about Jinjax, Htpy, etc. but im not familiar with any of them.
What do you guys use to create re-usable components in your flask app.
What are some good Flask tutorials that actually have good frontend UI?
I'm wanting to follow along with a tutorial that gets more in depth into an actual real use case instead of just a simple form
All youtube videos I can search are already old. Which resource do you recommend?
index.html works well while the apis return 404 error in vercel, can anyone help me
Hi, I'm new to Flask and have built a simple webapp to parse a schedule in raw text and add it to a google calendar. The app works perfectly in a virtual python environment, but I decided to add rate limiting with Redis and Docker, and since then have been swamped with issues. At first the site wouldn't even load due to issues with Redis. Now it does, but when I attempt to authenticate Google API credentials, I get this error: An error occurred: [Errno 98] Address already in use. Can anyone here help me solve this?
I currently am making an app that will query weather data from an AWS bucket and display it on a map. Right now I am using global variables to store progress data (small dictionary that records amount of files read, if program is running, etc) and the names of files that match certain criteria. However, I understand this is bad pratice for a web app. When trying to look for alternatives, I discovered flask's session, but my "results" variable will need to store anywhere from 50-100 filenames, with the possibility of having up to 2700. From my understanding this list of files seems like way too much data for a session variable. When I tested the code, 5 filenames was 120 bytes, so I think that its pretty impossible to stay under 4kb. Does anyone have any ideas instead? Once a user closes the tab, the data is not important (there are download functions for maps and files). I would perfer not to use a db, but will if that is outright the best option.
I'm trying to set up an email sending system. The problem is that if I set MAIL_SERVER and MAIL_PORT their values always remain None. How can I solve it?
Found the answer: as of jan/2025, if you install nginx following the instructions on Nginx.org for Ubuntu, it will install without nginx-common
and will never find any proxy_pass
that you provide. Simply install the version from the Ubuntu repositories and you will be fine.
Find the complete question below, for posterity.
Hi all.
I´m trying to install a Nginx/Gunicorn/Flask app (protocardtools is its name) in a local server following this tutorial.
Everything seems to work fine down to the last moment: when I run sudo nginx -t
I get the error "/etc/nginx/proxy_params" failed (2: No such file or directory) in /etc/nginx/conf.d/protocardtools.conf:22
Gunicorn seems to be running fine when I do sudo systemctl status protocardtools
Contents of my /etc/nginx/conf.d/protocardtools.conf
:
server {
listen 80;
server_name cards.proto.server;
location / {
include proxy_params;
proxy_pass http://unix:/media/media/www/www-protocardtools/protocardtools.sock;
}
}
Contents of my /etc/systemd/system/protocardtools.service
:
[Unit]
Description=Gunicorn instance to serve ProtoCardTools
After=network.target
[Service]
User=proto
Group=www-data
WorkingDirectory=/media/media/www/www-protocardtools
Environment="PATH=/media/media/www/www-protocardtools/venv/bin"
ExecStart=/media/media/www/www-protocardtools/venv/bin/gunicorn --workers 3 --bind unix:protocardtools.sock -m 007 wsgi:app
[Install]
WantedBy=multi-user.target
Can anyone please help me shed a light on this? Thank you so much in advance.
I am trying to run my web app via my windows hotspot on my laptop but the application seems unable to listen on the hotspot. I have tried listening on my laptops hotspot interface (192.168.137.1
) and all interfaces (0.0.0.0
) when listening on all interfaces my hotspot interface does not appear in the list. Is there a way to resolve this? Would this application work on the hotspot from a Raspberry Pi? Happy to provide selected code snippets as required but much of the code is sensitive so won't be uploaded in an uncensored form.
So I have a server which takes files from the user, process it and return the processed files back to the user.
For example, a user uploads 2 files, server process that 2 files and returns 2 new files back.
Now if there are 10 users using the application at the same time, sending 2 files each, how to make sure that they get back their respective files??
Edit: One way i can think if is using unique id to store each files in a separate directory or something of sort but is there any more efficient way to achieve this as i need to scale this application to atleast handle 1000 users at a time
I'm creating a web application following Miguel's mega-tutorial.
I'm on lesson 7 and I've encountered a problem: for some reason, despite having intentionally caused an error in my site, no message appears in the second terminal where aiosmtpd is running.
I don't understand why nothing appears. These are my bits of code that I used to try to make this stuff work:
in config file:
import os
basedir = os.path.abspath(os.path.dirname(__file__)) # Questa variabile immagazzina il percorso della directory principale sottoforma di stringa
class Config:
SECRET_KEY = os.environ.get('SECRET_KEY') or b'mykey'
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
'sqlite:///' + os.path.join(basedir, 'app.db')
MAIL_SERVER = os.environ.get('MAIL_SERVER')
MAIL_PORT = int(os.environ.get('MAIL_PORT') or 25)
MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS') is not None
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
ADMINS = ['mymail@example.com']
in init file:
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
import logging
from logging.handlers import SMTPHandler
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
login = LoginManager(app)
login.login_view = 'login'
if not app.debug: # Dopo devo controllare.
if app.config['MAIL_SERVER']:
auth = None
if app.config['MAIL_USERNAME'] or app.config['MAIL_PASSWORD']:
auth = (app.config['MAIL_USERNAME'], app.config['MAIL_PASSWORD'])
secure = None
if app.config['MAIL_USE_TLS']:
secure = ()
mail_handler = SMTPHandler(
mailhost=(app.config['MAIL_SERVER'], app.config['MAIL_PORT']),
fromaddr='no-reply@' + app.config['MAIL_SERVER'],
toaddrs=app.config['ADMINS'], subject='Microblog Failure',
credentials=auth, secure=secure)
mail_handler.setLevel(logging.ERROR)
app.logger.addHandler(mail_handler)
from app import routes, models, errors # Metto questa riga alla fine del file perché in routes e models importo alcune delle variabili sopra,
# per cui se importassi i due moduli all'inizio di questo file avrei problemi di importazione
recently i purchased a vps from hostinger but unfortunately there's no support for python flask but it allows various apps, panels, and plain OS as well. but i genuinely don't know what I'm doing. and I do want to connect a custom domain as well.
Hey Guys and Girls,
I´m wondering about a lot over Tutorials. I´m workin on my first little Flask Web App. I´m a student for Media Tech with intermediate or better good understanding whatsoever.
In many Tutorials this "Mapped" SQLALchemy 2.0 style just does not exist. Why is that? Is there any big difference?
The SQL ALchemy Tutorial tells me to use that style over the old style... I dont get it.
Or it is like Flask-alchemy is using the old style?
# SQL ALCHEMY 2.0 STYLE
class Base(DeclarativeBase):
pass
db = SQLAlchemy(model_class=Base)
class Sailor(Base):
__tablename__ = 'sailor'
id: Mapped[int] = mapped_column(primary_key=True)
username: Mapped[str] = mapped_column(String(50), nullable=False)
password: Mapped[str] = mapped_column(String(50), nullable=False)
#S SQL ALCHEMY OLD STYLE
class Sailor(db.base):
__tablename__ = 'sailor'
id = db.Column(db.Integer, primary_key = True)
etc....
Hi All,
Hi everyone,
I'm working on a small project involving web application development. While I can successfully create records for users, I'm running into trouble updating field values. Every time I try to update, I encounter a 304 Not Modified
status response.
I suspect there's something wrong in my code or configuration, but I can't pinpoint the exact issue.
Here’s what I’d like help with:
304 Not Modified
status.Below is a brief overview of the technologies I’m using and relevant details:
I’d appreciate any guidance or suggestions. If needed, I can share snippets of the relevant code. Thank you in advance!
I started reading Indico, but looks like it is mostly a server rendered app rather than an API and SPA. Are there are other large examples that I can read that use Flask and SQLAlchemy?
I created a python script that generates the "Turning LED on" text in a web browser using the Flask API. After finding success with printing the text to the web browser, I attempted to import the GPIOZERO library and use the script to literally turn on an LED. Unfortunately, I cannot get it to work. I would appreciate any help with troubleshooting the code.
I tried creating local variables under the defined method. Please see below:
----------------------------------------
from flask import Flask
from gpiozero import LED
app = Flask(__name__)
'@app*.route('/led/on')
def led_on():
....return "Turning on the LED"
....return {'status': True}
....led = LED(17)
....green.on()
'@app*.route('/led/off')
def led_off():
....return "Turning off the LED"
....led = LED(17)
....green.off()
-------------------------------------------
Thanks in advance for the help!
Hi, flask newbie here
i spent quite a while developing a react and flask application thats pretty simple,
react fronend, sends post requests and get requests to flask backend, uses routing aswell.
i had it up working fine with localhost:5000 and localhost:3000 and now I'm trying to deploy it,
tried to use Dockerfile with render and deploy them both at the same time, ended up being really frustrating so i switched to using a digitalocean droplet, and following a tutorial that got me setup with nginx, but because my flask backed doesnt really display anything i am having trouble debugging - i set up a system service on the droplet (just a linux vm) so a gunicorn process is alway running, but i cant visit it properly. also i have a domain but it doenst seemt o work with nginx.
any advice on deployment?
Thanks
from
flask
import
Flask, redirect, url_for, Blueprint, request, jsonify
from
myproject
import
db
from
myproject.manager.models
import
Manager, Events
from
flask_jwt_extended
import
create_access_token, jwt_required, get_jwt_identity
from
datetime
import
datetime
import
zipfile
import
os
from
io
import
BytesIO
from
werkzeug.utils
import
secure_filename
manager_bp = Blueprint('manager_bp' ,__name__)
@manager_bp.route('/sign-up/',
methods
=['POST'])
def sign_up():
data = request.json
email = data.get('email')
password_h = data.get('password')
if
not email or not password_h:
return
jsonify({'success': False, 'error': 'Missing required fields'}), 400
existing_manager = Manager.query.filter_by(
email
=email).first()
if
existing_manager:
return
jsonify({'success': False, 'error': 'manager with this email already exists'}), 400
new_manager = Manager(
email
= email ,
password
= password_h)
db.session.add(new_manager)
db.session.commit()
# Generate JWT Token
access_token = create_access_token(
identity
=email)
return
jsonify({"success": True, "message": "Manager created successfully!", "access_token": access_token}), 201
@manager_bp.route('/login' ,
methods
=['POST'])
def login():
data = request.json
email = data.get('email')
password = data.get('password')
if
not email or not password:
return
jsonify({'error': 'Email and password are required'}), 400
manager = Manager.query.filter_by(
email
=email).first()
if
manager is None or not manager.check_password(password):
return
jsonify({'error': 'Invalid email or password'}), 401
access_token = create_access_token(
identity
=email)
return
jsonify({'success': True, 'access_token': access_token}), 200
@manager_bp.route('/logout',
methods
=['POST'])
@jwt_required()
def logout():
return
jsonify({'message': 'Successfully logged out, but the token is invalidated on client side.'}), 200
@manager_bp.route('/create-event',
methods
=['POST'])
@jwt_required()
def create_event():
data = request.json
event_name = data.get('event_name')
description = data.get('description')
date_str = data.get('date')
# YYYY-MM-DD
organised_by = data.get('organised_by')
if
not event_name or not description or not date_str:
return
jsonify({'success': False, 'error': 'Missing required fields'}), 400
try
:
date = datetime.strptime(date_str, '%Y-%m-%d').date()
except
ValueError:
return
jsonify({'success': False, 'error': 'Invalid date format. Use YYYY-MM-DD.'}), 400
new_event = Events(
event_name
=event_name,
description
=description,
date
=date,
organised_by
=organised_by)
db.session.add(new_event)
db.session.commit()
return
jsonify({"success": True, "message": "Event created successfully!"}), 201
@manager_bp.route('/events',
methods
=['GET'])
@jwt_required()
def get_events():
events = Events.query.all()
events_list = []
for
event
in
events:
events_list.append({
'event_name': event.event_name,
'description': event.description,
'date': event.date.strftime('%Y-%m-%d'),
'organised_by': event.organised_by
})
return
jsonify(events_list), 200
@manager_bp.route('/event-details',
methods
=['GET', 'POST'])
@jwt_required()
def get_event_details():
if
request.method == 'GET':
event_name = request.args.get('event_name')
else
:
# POST
data = request.json
event_name = data.get('event_name')
print(f"Received event_name: {event_name}")
# Debug statement
if
not event_name:
return
jsonify({'success': False, 'error': 'Event name is required'}), 400
event = Events.query.filter_by(
event_name
=event_name).first()
if
not event:
return
jsonify({'success': False, 'error': 'Event not found'}), 404
return
jsonify({
'success': True,
'event': {
'event_name': event.event_name,
'description': event.description,
'date': event.date,
'organised_by': event.organised_by
}
}), 200
@manager_bp.route('/upload-images',
methods
=['POST'])
@jwt_required()
def upload_images():
# Validate content type and file presence
# if not request.content_type.startswith('multipart/form-data'):
# return jsonify({'success': False, 'error': 'Content-Type must be multipart/form-data'}), 415
if
'file' not in request.files:
return
jsonify({'success': False, 'error': 'No file part'}), 400
file = request.files['file']
if
file.filename == '':
return
jsonify({'success': False, 'error': 'No selected file'}), 400
if
not zipfile.is_zipfile(file):
return
jsonify({'success': False, 'error': 'File is not a zip file'}), 400
event_name = request.form.get('event_name')
if
not event_name:
return
jsonify({'success': False, 'error': 'Event name is required'}), 400
event = Events.query.filter_by(
event_name
=event_name).first()
if
not event:
return
jsonify({'success': False, 'error': 'Event not found'}), 404
# Create a secure directory for extraction
extract_path = os.path.join('uploads', secure_filename(event_name))
os.makedirs(extract_path,
exist_ok
=True)
try
:
# Extract the zip file
with
zipfile.ZipFile(file, 'r')
as
zip_ref:
# Validate files before extraction
invalid_files = [f
for
f
in
zip_ref.namelist()
if
not f.lower().endswith(('.png', '.jpg', '.jpeg'))]
if
invalid_files:
return
jsonify({
'success': False,
'error': f'Invalid files found: {", ".join(invalid_files)}. Only .png, .jpg, .jpeg allowed.'
}), 400
zip_ref.extractall(extract_path)
# Process each image
embeddings = []
for
root, dirs, files
in
os.walk(extract_path):
for
filename
in
files:
if
filename.lower().endswith(('.png', '.jpg', '.jpeg')):
image_path = os.path.join(root, filename)
# Generate embedding for the image
try
:
embedding = generate_embedding(image_path)
embeddings.append({
'event_id': event.id,
'image_name': filename,
'embedding': embedding
})
except
Exception
as
e:
print(f"Error processing {filename}: {str(e)}")
# Optionally, you could choose to continue or return an error
# Batch insert embeddings
if
embeddings:
bulk_embeddings = [
ImageEmbedding(
event_id
=emb['event_id'],
image_name
=emb['image_name'],
embedding
=emb['embedding']
)
for
emb
in
embeddings
]
db.session.bulk_save_objects(bulk_embeddings)
db.session.commit()
return
jsonify({
"success": True,
"message": f"Processed {len(embeddings)} images successfully!",
"total_images": len(embeddings)
}), 201
except
Exception
as
e:
db.session.rollback()
return
jsonify({
'success': False,
'error': f'Upload processing failed: {str(e)}'
}), 500
finally
:
# Clean up extracted files
if
os.path.exists(extract_path):
import
shutil
shutil.rmtree(extract_path)
def generate_embedding(
image_path
):
"""
Generate image embedding using a machine learning model.
Replace with your actual embedding generation logic.
Args:
image_path (str): Path to the image file
Returns:
list: Image embedding vector
"""
# Example placeholder - replace with actual embedding generation
from
PIL
import
Image
import
numpy
as
np
# Open and preprocess image
img = Image.open(
image_path
)
img = img.resize((224, 224))
# Typical size for many ML models
# Dummy embedding generation
# In real-world, you'd use a pre-trained model like ResNet or VGG
return
np.random.rand(128).tolist()
Hello, this the manager/views.py, I want to get a zip file and event_name from front-end and this will unzip the file, generate embeddings of all the images and store is in the db, in the form of event_name_1.png.....
when I try to do this using Postman, I get this :
Please can someone help me here.
Thankyou so much for your time
Over the past few weeks , I’ve been delving into Flask web development, and the progress has been incredibly rewarding. I’ve implemented user registration and login with secure password hashing, added TOTP-based OTP verification to ensure account security, and integrated Flask-Mail for sending verification emails.
Managing database models with sqlalchemy has been a game changer for me. Initially I resorted to Cs50's SQL which was way cooler. But the SQLAlchemy integrates better with flask as I've come to experience. I’ve also added custom logging to track user actions like logins, OTP verification, and profile updates.
It's been mostly Trial and error but it's been fun seeing the understanding I'm getting about how websites work under the hood just by building one😃
In addition to my question above, what more can I implement with flask to make my web app more secure if deployed on the web...
I would really appreciate your input🙏🏿
Newbie in Deployment: Need Help with Managing Load for FastAPI + Qdrant Setup
I'm working on a data retrieval project using FastAPI and Qdrant. Here's my workflow:
User sends a query via a POST API.
I translate non-English queries to English using Azure OpenAI.
Retrieve relevant context from a locally hosted Qdrant DB.
I've initialized Qdrant and FastAPI using Docker Compose.
Question: What are the best practices to handle heavy load (at least 10 requests/sec)? Any tips for optimizing this setup would be greatly appreciated!
Please share Me any documentation for reference thank you
Hello i am still relatively new to programming and developed a python flask app that uses openai api call to respond to user input. My application works fine locally but continues to crash during the build whenever i try to host it. Ive tried Vercel as well as Digital Ocean and Fly.io