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
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
Building large application using Flask, Celery with https://github.com/gofynd/flask-full

Flask\-Full \([https://github.com/gofynd/flask\-full](https://github.com/gofynd/flask-full)\) is a boilerplate framework on top of flask for developing large applications using flask. It has in built support for shell commands, celery, websocket, eventlet, signals, mongoengine orm and rst docs. One can also have separate staging, production config.

/r/flask
https://redd.it/8ng29z
Want to learn to populate Django objects with JSON data

I've got a web scraper that returns some basic JSON data, and I've got a Django site that has models roughly corresponding to that data.

I've googled all over for tutorials on how to do populate my Django model with JSON data, and there's lots of people with partial explanations, but nobody with a full tutorial.

I know django-dynamic-scraper exists, but its documentation is not clear enough to get it working. I've been struggling with this for a few weeks now. Anybody have experience with this? Any chance there's a good tutorial I missed somewhere?

/r/djangolearning
https://redd.it/8pcz6y
Return Image as API response.

What is the best way to return image from flask API to fetch in mobile/web app? I was thinking to develop a react app which will call api and get a response with filename and it will return image from file system? Is this how its done, i don't have much idea about react just starting personal project.

/r/flask
https://redd.it/8pj0bg
[AF] Trouble with Flask and using ajax to dynamically refresh

I'm very new to this so I'm really unsure why this doesn't work as I was following the answer to [this question](https://stackoverflow.com/questions/15721679/update-and-render-a-value-from-flask-periodically/). I'm trying to post a form to flask where I process it as needed. This part works fine but when I'm trying to return it back to my html file I notice the $.getJSON part simply doesn't run. Any help or advice is appreciated.

The form part in my index.html:

<!-- POST Form -->
<form action='/endpoint' name="entry" id="entry" method="POST" target="_blank">
<textarea placeholder="Enter Cipher..." id="input" type="text" name="entry" class="inputbox">{{original_text}}</textarea>
<br />
<br />
<input type="submit" class="form__button" value="DECRYPT" onclick="decrypt_click()" />
<br />
<br />
</form>

<textarea id="output" type="text" name="output" class="inputbox" readonly>{{deciphered_text}}</textarea>

Javascript in my html file:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript">
function update_values() {
$SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
$.getJSON(
$SCRIPT_ROOT+"/endpoint",
function(data) {
$("textarea#output").val(data.deciphered_text)
$("textarea#entry").val(data.deciphered_text)
}
);
setTimeout(update_values, 1000);
}
update_values()
</script>

My ____init____.py:

from flask import Flask, render_template, request, jsonify, redirect, url_for
import sys
app = Flask(__name__)
from FlaskApp.autocrack import crack_vigenere, crack_caesar, crack_substitution, crack_beaufort, crack_2x2hill, crack_3x3hill, crack_bifid
import re

@app.route('/')
def homepage():
return render_template('index.html',deciphered_text="",original_text="",checkbox_status="1")

@app.route('/endpoint', methods=['GET','POST'])
def entry():
if request.method == 'POST':
text_orig = request.form['entry']
text = removeFormatting(text_orig)
cipher = request.form.get('cipher_selection_menu')
cipher = request.form['selection']
if text != "":
processed_text = text.upper()
if cipher == "1":
deciphered_text = crack_caesar(processed_text)
elif cipher == "2":
deciphered_text = crack_vigenere(processed_text)
elif cipher == "3":
timeout = request.form['timeout_slider']
deciphered_text = crack_substitution(processed_text,timeout)
elif cipher == "4":
deciphered_text = crack_beaufort(processed_text)
elif cipher == "5":
type = request.form['selector']
if type == "2":
deciphered_text = crack_2x2hill(processed_text)
elif type == "3":
deciphered_text = crack_3x3hill(processed_text)
elif cipher == "6":
deciphered_text = crack_bifid(processed_text)
return jsonify(deciphered_text=deciphered_text,original_text=text_orig)
return redirect(url_for('homepage'))

def removeFormatting(stringf):
return re.sub("[^a-zA-Z]+", "", stringf)

if __name__ == "__main__":
#app.run()
app.run(threaded=True)

/r/flask
https://redd.it/8pn4uu
[AF] Ext to show flask cli commands as a web app?

I don't know if I was dreaming or just making things up.

But I believe there is somewhere a flask ext that gives you a kind of a menu as a web app to enter parameters and then execute the flask.cli function?

Any thoughts or opinions?

/r/flask
https://redd.it/8pmwyu