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
Login through api

Hello guys what would be the best way to login through a api and to keep the user logged in like flask-login do and that can offer the best protection for the api

/r/flask
https://redd.it/8p5cnn
Flask in Docker, some problems here.

Hello everyone,
once again i have a problem with a docker app. To be more precise i want to learn python flask development, so i created an docker image, here it is:
`FROM python:alpine`
`WORKDIR /app`
`COPY ./app/requirements.txt .`
`RUN pip install --no-cache-dir -U -r requirements.txt`
Pretty basic. Just installs flask into my container.
Then i have the following docker\-compose file:
`version: '3'`
`services:`
`flaskapp:`
`build: .`
`container_name: flaskapp`
`volumes:`
`- $PWD/app:/app`
`environment:`
`- FLASK_APP=app.py`
`- FLASK_SETTINGS=config/config.py`
`ports:`
`- "5000:5000"`
`command: flask run`
As you see, there are two environment vars that i want to pass to my container. First, the actual flask app that is in the /app folder. Then, the localtion of my config file which is in the /config directory.
Here is my app.py file:
`from flask import Flask`
`import os`
`app = Flask(__name__)`
`app.config.from_envvar('FLASK_SETTINGS')`
`@app.route('/')`
`def index():`
`return 'Index Page'`
Here is the config file:
`DEBUG = True`
`HOST = '0.0.0.0'`
So now to the actual misbehavior, when i start the container i get the following:
`docker-compose up`
`Starting flaskapp ... done`
`Attaching to flaskapp`
`flaskapp | * Serving Flask app "app.py"`
`flaskapp | * Environment: production`
`flaskapp | WARNING: Do not use the development server in a production environment.`
`flaskapp | Use a production WSGI server instead.`
`flaskapp | * Debug mode: off`
`flaskapp | * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)`
The environment variable FLASK\_APP is being read correctly, flask detects its ("`* Serving Flask app "`[`app.py`](https://app.py/)`"`"), but the config file isnt loaded ("`Debug mode: off`"), also `HOST = '`[`0.0.0.0`](https://0.0.0.0/)`'` is not set because i cant reach the app with my browser.

I assume that i have some error in my [app.py](https://app.py), but iam not sure.

Can you guys see any errors in my set up here? Wanted to learn flask and now already stuck with the basics :(

Btw, i also tried it without docker (same python files), also no luck.

Thanks in advance, have a nice day everyone!

/r/flask
https://redd.it/8p2ygx
Advice on IDE or editor for Django

I'm new at Django and currently working on a large personal project (iOS chat app with django backend). I'm using Webfaction hosting, and right now I'm just logging in through ssh, using vim to edit files, then running and opening up the error log file to debug one error at a time. Is there better way of doing this? It'd be nice to be able to use a Python "compiling" editor and run the code immediately without having to ssh files back and forth.

/r/django
https://redd.it/8p6zoa
Easiest way to query a model's property?

In my user model, I have a paid member ("premium") property that checks Stripe and returns True or False.

This is fine for 90% of my site and locking away content, but there are also times when I need to sort users by premium status. Any suggestions for how to do this?

I saw something along the lines of this:

class UserProfile(models.model):
premium_field = models.BooleanField(db_column="premium")

@property
def premium(self):
if stripe.paid():
return True
else:
return False


But it looks like I might need a getter function as well? Hopefully I'm on the right track -- I just don't want to migrate or push changes unless I know I have it right. Thanks!



/r/djangolearning
https://redd.it/8p57w5
Is it a good idea to use a mixin for a multi-value field?

Lets say that I have a model called 'Movie' and I want 'Movies' to have a spot to remember all the genres it belongs to (Horror, Suspense, Funny, Romance, etc... ). It seems like there are a few options. I could make a single field that stores a JSON string that holds genres as keys and booleans as values (Bad Idea). I could create a Boolean field in 'Movie' for each of the genres. But that makes for a lot of fields in a single model. Or I could make a mixin called 'GenreMixin' with a boolean field for each genre and have 'Movie' inherit from it.


What is the best practice, what will lead to the easiest to maintain down the road?

/r/djangolearning
https://redd.it/8p538x
[AF] How to run a function after returning response (AWS Lambda)

Hey everyone! I'm using zappa to run a simple ffmpeg transcode function on Lambda with Flask. While Lambda functions can run for up to 5 minutes, the problem I'm running into is that Amazon's API Gateway times out after 30 seconds so I'm unable to just wait for ffmpeg to finish before returning a response.

I'm looking for a way to return a response immediately from my Flask function and then continue running my ffmpeg function in the background. (The Flask response will contain a JSON payload with the S3 url that the finished ffmpeg job will upload to.)

I've tried using `threading.Thread` but it doesn't appear to stay alive after my Flask function returns. Thoughts/ideas? Thank you for your help!

/r/flask
https://redd.it/8p78ev
How do you use Python to automate something in your daily life?

This question seems to produce new and unique responses each time it is asked, so why not give it another go? I’d love to hear how you use Python to automate something to save you time!

/r/Python
https://redd.it/8p7zjf
[AF] How to sort SQLAlchemy query by number of many to many relationships

Hello!

I have two tables which I've simplified below. A post model and a post likes model. When I want to create a like on a post, I create an instance of a post like which holds a backreference to the post in question.

I would like to perform a query that will grab all my posts and sort them by number of likes. However, because I'm still a bit green when it comes to SQLAlchemy, I'm not sure how to sort by number of many to many relationships (self\_likes).

class PostLike(db.Model):
post_id = db.Column("post_id", db.Integer, db.ForeignKey("post.id"),
primary_key=True)

class Post(db.Model):
_tablename_ = "post"
id = db.Column(db.Integer, primary_key=True)
self_likes = db.relationship("PostLike", backref="self_likes", lazy="dynamic", foreign_keys="PostLike.post_id")

Any help would be greatly appreciated!

/r/flask
https://redd.it/8p5wn6
Jupyter temp files not being deleted question.

Hi Everyone,
On my work PC, I run Jupyter 5.4.0, usually through Anaconda Navigator. I have always had an issue where the temp files created during a session do not get deleted. These are located in my AppData\Local\temp folder. The size of each of the files is 0 bytes, but, if I let it go for a few days, I can get thousands of 0 bytes files. I believe it is related to Jupyter but I am not 100% certain. I know that if I run Python through any other IDE I don't see the same thing. Is there a way to tell if this is a result of Jupyter? More importantly, if it is, how do I configure it to stop this?

Thanks for the input!

/r/IPython
https://redd.it/8pelmg
Override Flask Login to use Request Loader

So here's my situation, I have an app which I can't edit, but I can add code to it. At the moment it uses Flask Login for user management/authentication. I want to use Flask Login's Request Loader.

I want to use one of AWS"s new features where I can authenticate on an Application Load Balancer, which will pass the user information to my application via headers. I therefore want to inspect the headers and either create a session on the server for that user, or check the session and continue with the request if the user already has a session.

So my question is this, how would I go about overriding the current implementation of Flask Login? I know I can use decorators to do this, I just don't know which methods to override.

I basically know that this is possible, based on the docs, but I have no clue how to implement it.

/r/flask
https://redd.it/8p0tzn
What does """ do?

Automate the Boring Stuff has a program that changes American dates to European dates. The first part of the code goes:

datePattern = re.compile(r"""\^(.\*?)

#one or two digits for the month

((0|1)?\\d)\-

#one or two dgits for the day

((0|1|2|3)?\\d)\-

#four digits for the year

((19|20)\\d\\d)

#all text after the date

(.\*?)$

""", re.VERBOSE)

I have a pretty vague idea of what everything does except for the r"""\^ part on the first line. what does """ do?

/r/Python
https://redd.it/8pghas