Dynamically set variable in route wrapper
I'm using Flask-Limiter and would like to set the cost parameter on a per request basis. I can't figure out a way to get a variable passed to this parameter that would be dependent on the request. If I just pass in current\_user.zipcode\_changes I get an error saying I can't pass None type in.
[https://flask-limiter.readthedocs.io/en/stable/api.html#flask\_limiter.Limiter.limit.params.cost](https://flask-limiter.readthedocs.io/en/stable/api.html#flask_limiter.Limiter.limit.params.cost)
​
My attempt always returns 1.
The gist of my code:
def func_cost():
if current_user:
return current_user.zipcode_changes
else:
return 1
@bp.route('/zipocdes', methods=['GET','POST']
@limiter.limit("10 per day", key_func=lambda: current_user.id, methods=["POST"],\
deduct_when=func_deduct, cost=func_cost)
@login_required
def zipcodes():
form = ZipcodeForm()
if form.validate_on_submit():
current_user.zipcode_changes = len(added_zips)
/r/flask
https://redd.it/szlzi7
I'm using Flask-Limiter and would like to set the cost parameter on a per request basis. I can't figure out a way to get a variable passed to this parameter that would be dependent on the request. If I just pass in current\_user.zipcode\_changes I get an error saying I can't pass None type in.
[https://flask-limiter.readthedocs.io/en/stable/api.html#flask\_limiter.Limiter.limit.params.cost](https://flask-limiter.readthedocs.io/en/stable/api.html#flask_limiter.Limiter.limit.params.cost)
​
My attempt always returns 1.
The gist of my code:
def func_cost():
if current_user:
return current_user.zipcode_changes
else:
return 1
@bp.route('/zipocdes', methods=['GET','POST']
@limiter.limit("10 per day", key_func=lambda: current_user.id, methods=["POST"],\
deduct_when=func_deduct, cost=func_cost)
@login_required
def zipcodes():
form = ZipcodeForm()
if form.validate_on_submit():
current_user.zipcode_changes = len(added_zips)
/r/flask
https://redd.it/szlzi7
Flask-Limiter
API
Extension: Limit objects: The following dataclasses can be used to define rate limits with more granularity than what is available through the Limiter constructor if needed (especially for default,...
What is the point of request.content_md5 / Content-MD5 Header?
Flask has a `request.content_md5` which corresponds to the `Content-MD5` header. The [docs](https://flask.palletsprojects.com/en/2.2.x/api/#flask.Request.content_md5) say:
> The Content-MD5 entity-header field, as defined in RFC 1864, is an MD5 digest of the entity-body for the purpose of providing an end-to-end message integrity check (MIC) of the entity-body. (Note: a MIC is good for detecting accidental modification of the entity-body in transit, but is not proof against malicious attacks.)
What is the point of this?
I tried sending a request with a bogus value in the Content-MD5 header, and flask did not automatically fail the request.
Am I supposed to write some code that computes the hash of the request body and compare it against Content-MD5, if this is present, and then ask my clients to compute the MD5 and put it in the Content-MD5 header?
I would have assumed that clients would automatically do this as part of the client library, and the flask would automatically compare the md5 and fail with an error. It seems like this does not happen.
/r/flask
https://redd.it/xatptz
Flask has a `request.content_md5` which corresponds to the `Content-MD5` header. The [docs](https://flask.palletsprojects.com/en/2.2.x/api/#flask.Request.content_md5) say:
> The Content-MD5 entity-header field, as defined in RFC 1864, is an MD5 digest of the entity-body for the purpose of providing an end-to-end message integrity check (MIC) of the entity-body. (Note: a MIC is good for detecting accidental modification of the entity-body in transit, but is not proof against malicious attacks.)
What is the point of this?
I tried sending a request with a bogus value in the Content-MD5 header, and flask did not automatically fail the request.
Am I supposed to write some code that computes the hash of the request body and compare it against Content-MD5, if this is present, and then ask my clients to compute the MD5 and put it in the Content-MD5 header?
I would have assumed that clients would automatically do this as part of the client library, and the flask would automatically compare the md5 and fail with an error. It seems like this does not happen.
/r/flask
https://redd.it/xatptz
Nesting Blueprints with Blueprint.registerblueprint() fails
Even though the [Flask Docs](https://flask.palletsprojects.com/en/2.2.x/api/#flask.Blueprint.registerblueprint) state that a Blueprint can register further blueprints, I get the following error:
>AttributeError: 'Blueprint' object has no attribute 'register_blueprint'
Does someone know how I can achieve nesting blueprints?
Code
/r/flask
https://redd.it/100qn8a
Even though the [Flask Docs](https://flask.palletsprojects.com/en/2.2.x/api/#flask.Blueprint.registerblueprint) state that a Blueprint can register further blueprints, I get the following error:
>AttributeError: 'Blueprint' object has no attribute 'register_blueprint'
Does someone know how I can achieve nesting blueprints?
Code
/r/flask
https://redd.it/100qn8a
Learn Restful API with Flask
๐ Mastering RESTful APIs with Python and Flask ๐
Are you ready to take your skills to the next level? I'm excited to announce the launch of my new Udemy course, designed to help you achieve your goals and excel in your field!๐ Course Highlights:
* Comprehensive, easy-to-follow lessons
* Practical exercises and real-world examples
* Learn at your own pace, from anywhere
๐ก Why Enroll?Whether you're a beginner looking to get started or a seasoned professional seeking to refine your skills, this course has something for everyone. Gain in-depth knowledge, practical skills, and the confidence to succeed.
๐ Enroll Now: [https://www.udemy.com/course/mastering-restful-apis-a-comprehensive-guide/?referralCode=727DF5F0596A9494D3B5](https://www.udemy.com/course/mastering-restful-apis-a-comprehensive-guide/?referralCode=727DF5F0596A9494D3B5)
Join me on this exciting learning journey and unlock your full potential. See you in the course!
#Udemy #Python #flask #OnlineLearning #SkillDevelopment #Education #CourseLaunch #LearnFromHome
/r/flask
https://redd.it/1dnamd8
๐ Mastering RESTful APIs with Python and Flask ๐
Are you ready to take your skills to the next level? I'm excited to announce the launch of my new Udemy course, designed to help you achieve your goals and excel in your field!๐ Course Highlights:
* Comprehensive, easy-to-follow lessons
* Practical exercises and real-world examples
* Learn at your own pace, from anywhere
๐ก Why Enroll?Whether you're a beginner looking to get started or a seasoned professional seeking to refine your skills, this course has something for everyone. Gain in-depth knowledge, practical skills, and the confidence to succeed.
๐ Enroll Now: [https://www.udemy.com/course/mastering-restful-apis-a-comprehensive-guide/?referralCode=727DF5F0596A9494D3B5](https://www.udemy.com/course/mastering-restful-apis-a-comprehensive-guide/?referralCode=727DF5F0596A9494D3B5)
Join me on this exciting learning journey and unlock your full potential. See you in the course!
#Udemy #Python #flask #OnlineLearning #SkillDevelopment #Education #CourseLaunch #LearnFromHome
/r/flask
https://redd.it/1dnamd8
Udemy
Mastering RESTful APIs with Python and Flask
From Design to Deployment: Unlock the Power of RESTful APIs for Seamless Web Integration
How to setup a permanent session in default Flask?
If I use Flask-Session to store session data server-side, it is as easy as passing
If I use default Flask to store session data client-side, I need to set
What I don't understand is how to setup the default Flask one. I tried to google, and found this in Stack Overflow. The accepted answer suggests setting it with before\_request, but I think why every request, we need to set
1. How to set permanent sessions in default flask properly? Where to set it?
2. Can you toggle permanent and non permanent session?
3. What should we do after session is expired? What about the session data?
/r/flask
https://redd.it/1gdur94
If I use Flask-Session to store session data server-side, it is as easy as passing
SESSION_PERMANENT = False (not default Flask, unique to Flask-Session) to the app.config, then set the desired expiration time with , PERMANENT_SESSION_LIFETIME = desired_exp_time which is default Flask config. If I use default Flask to store session data client-side, I need to set
PERMANENT_SESSION_LIFETIME = desired_exp_time first, then state session.permanent = True in the request (ex. u/app.route), else I get this error: RuntimeError: Working outside of request context. The docs is somewhat unclear about how to implement, see here.What I don't understand is how to setup the default Flask one. I tried to google, and found this in Stack Overflow. The accepted answer suggests setting it with before\_request, but I think why every request, we need to set
session.permanent = True?1. How to set permanent sessions in default flask properly? Where to set it?
2. Can you toggle permanent and non permanent session?
3. What should we do after session is expired? What about the session data?
/r/flask
https://redd.it/1gdur94
Stack Overflow
Flask permanent session: where to define them?
By default, Flask uses volatile sessions, which means the session cookie is set to expire when browser closes. In order to use permanent sessions, which will use a cookie with a defined expiratio...
Flask Async Mail
๐ Introducing Flask-Async-Mail! ๐ง
Hey everyone! I just released Flask-Async-Mail, a lightweight and flexible asynchronous email-sending library for Flask apps using Celery. ๐
๐น Features:
โ Supports both synchronous & asynchronous email sending
โ Works with Celery & Redis
โ Supports plaintext & HTML emails
โ Simple setup & easy integration with Flask
๐ Try it out & contribute!
๐ฆ PyPI: https://pypi.org/project/flask-async-mail/
๐ป GitHub: https://github.com/manitreasure1/flas-async-mail.git
Iโd love your feedback, contributions, and โญ stars on GitHub! Letโs build something awesome together. ๐๐ฅ
\#Flask #Python #Async #Email #OpenSource
/r/flask
https://redd.it/1j10wib
๐ Introducing Flask-Async-Mail! ๐ง
Hey everyone! I just released Flask-Async-Mail, a lightweight and flexible asynchronous email-sending library for Flask apps using Celery. ๐
๐น Features:
โ Supports both synchronous & asynchronous email sending
โ Works with Celery & Redis
โ Supports plaintext & HTML emails
โ Simple setup & easy integration with Flask
๐ Try it out & contribute!
๐ฆ PyPI: https://pypi.org/project/flask-async-mail/
๐ป GitHub: https://github.com/manitreasure1/flas-async-mail.git
Iโd love your feedback, contributions, and โญ stars on GitHub! Letโs build something awesome together. ๐๐ฅ
\#Flask #Python #Async #Email #OpenSource
/r/flask
https://redd.it/1j10wib
Need Career Advice: Stuck in .NET Web Forms, Should I Switch to Python Flask?
Hi everyone,
Iโve been working at a company for the past 4 months. I was hired to work on a .NET Web Forms project, but the pace of work is extremely slow. For the last 3 months, I havenโt written any real code โ Iโve just been learning about Web Forms.
The company is saying theyโll give me actual work on an ERP project starting next week, but honestly, Iโm not feeling confident. Iโve been told there will be no proper mentorship or guidance, and I find ERP systems really hard to grasp.
On the other hand, Iโm passionate about innovation and working with new technologies. I really enjoy Python and Iโve been considering switching over to Flask development instead, since it aligns more with what I want to do in the future.
Iโm feeling a lot of stress and confusion right now. Should I stick it out with this company and the ERP/.NET stuff, or should I start focusing on Python Flask and make a shift in that direction?
Any advice from experienced developers would be really appreciated. Thanks!
\#CareerAdvice #DotNet #Python #Flask #ERP #WebForms #JuniorDeveloper #ProgrammingHelp
/r/flask
https://redd.it/1mbebbe
Hi everyone,
Iโve been working at a company for the past 4 months. I was hired to work on a .NET Web Forms project, but the pace of work is extremely slow. For the last 3 months, I havenโt written any real code โ Iโve just been learning about Web Forms.
The company is saying theyโll give me actual work on an ERP project starting next week, but honestly, Iโm not feeling confident. Iโve been told there will be no proper mentorship or guidance, and I find ERP systems really hard to grasp.
On the other hand, Iโm passionate about innovation and working with new technologies. I really enjoy Python and Iโve been considering switching over to Flask development instead, since it aligns more with what I want to do in the future.
Iโm feeling a lot of stress and confusion right now. Should I stick it out with this company and the ERP/.NET stuff, or should I start focusing on Python Flask and make a shift in that direction?
Any advice from experienced developers would be really appreciated. Thanks!
\#CareerAdvice #DotNet #Python #Flask #ERP #WebForms #JuniorDeveloper #ProgrammingHelp
/r/flask
https://redd.it/1mbebbe
Reddit
From the flask community on Reddit
Explore this post and more from the flask community
app.run() not recommended for development server?
For me it would be convenient to run the Flask development server with
> It is not recommended to use this function for development with automatic reloading as this is badly supported. Instead you should be using the flask command line scriptโs run support.
Documentation link: https://flask.palletsprojects.com/en/stable/api/#flask.Flask.run
Instead they suggest to use the command line
But I wonder how its different. Why is Flask.run not recommended?
/r/flask
https://redd.it/1p9t86o
For me it would be convenient to run the Flask development server with
Flask.run() however the documentation says:> It is not recommended to use this function for development with automatic reloading as this is badly supported. Instead you should be using the flask command line scriptโs run support.
Documentation link: https://flask.palletsprojects.com/en/stable/api/#flask.Flask.run
Instead they suggest to use the command line
flask run which I currently do.But I wonder how its different. Why is Flask.run not recommended?
/r/flask
https://redd.it/1p9t86o
Reddit
From the flask community on Reddit
Explore this post and more from the flask community