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
Tuesday Daily Thread: Advanced questions

Have some burning questions on advanced Python topics? Use this thread to ask more advanced questions related to Python.

If your question is a beginner question we hold a beginner Daily Thread tomorrow (Wednesday) where you can ask any question! We may remove questions here and ask you to resubmit tomorrow.

This thread may be fairly low volume in replies, if you don't receive a response we recommend looking at r/LearnPython or joining the Python Discord server at https://discord.gg/python where you stand a better chance of receiving a response.

/r/Python
https://redd.it/13ip05k
Introducing seaborn-polars, a package allowing to use Polars DataFrames and LazyFrames with Seaborn

In the last few months I've been using Polars more and more with only major inconvenience being that when doing exploratory data analysis, Polars dataframes are not supported by any of the common plotting packages. So it was either switching to Pandas or having a whole lot of boilerplate. For example, creating a scatterplot using pandas df is simply:

import seaborn as sns
sns.scatterplot(df, x='rating', y='votes', hue='genre')


But with Polars you'd have to do:

x = df.select(pl.col('rating')).to_numpy().ravel()
y = df.select(pl.col('votes')).to_numpy().ravel()
hue = df.select(pl.col('genre')).to_numpy().ravel()
sns.scatterplot(x=x, y=y, hue=hue)


That's quite a lot of boilerplate so I wrote this small package that is a wrapper around seaborn plotting functions and allows for them to be used with Polars DataFrames and LazyFrames using the same syntax as with Pandas dfs:

import polars as pl
import seaborn_polars as snl

df = pl.scan_csv('data.txt')
snl.scatterplot(df, x='rating', y='votes', hue='genre')


The code creates a deepcopy of the original dataframe so your source LazyFrames will remain lazy after plotting.

The package is available on PyPI: https://pypi.org/project/seaborn-polars/

If you want to contribute or interested in source code, the repository is here: https://github.com/pavelcherepan/seaborn\_polars

/r/Python
https://redd.it/13ittrl
Python Flask routing return 404 in my hosting (Centos 7 Cpanel)



Hi,

I’ve got an issue in terms of using send_from_directory in my app.py :

​

from flask import Flask, render_template, request, jsonify, send_from_directory

import json

app = Flask(__name__)

u/app.route("/")

def home():

return render_template("index.html")

u/app.route("/newIndex", methods=["GET", "POST"])

def newIndex():

Appdata = extractApp(appUrl)

json.dumps(Appdata)

filename = Appdata["nama_file"].replace("./static/assets/save\\", "")

permitted_directory = "static/assets/save/"

return send_from_directory(

directory=permitted_directory, path=filename, as_attachment=True

)

u/app.route("/riwu")

def riwu():

return "<h1>ini test saja 123</h1>"

if __name__ == "__main__":

`app.run`(debug=True)

&#x200B;



However, when i deploy to my hosting (using centos 7 in Cpanel), the @app.route(“/”) it’s works as well as if i type manually in http://[my_website\]/riwu. but not for @app.route(“/newIndex”).

it shows error “Not found : The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.”.

I already setup the .htaccess and httpd.conf to change value from :

&#x200B;



<Directory /var/www/html/public>

...

AllowOverride None

...

</Directory>

&#x200B;

to

&#x200B;



<Directory /var/www/html/public>

...

AllowOverride All

...

</Directory>

&#x200B;



but still does not work.

I’ve tried in my local server using Visual Code (Windows 10) and it’s works normally.

note : @ app.route(“/newIndex”) is does not have any redirect new .html , It just to download file from send_from_directory.

&#x200B;

is there any configuration that i missed?

Thanks

/r/flask
https://redd.it/13j7jv3
I made a small Website where everyone can leave a little note. I am not the most experienced, so this project was a lot of fun.

/r/flask
https://redd.it/13imus9
Wednesday Daily Thread: Beginner questions

New to Python and have questions? Use this thread to ask anything about Python, there are no bad questions!

This thread may be fairly low volume in replies, if you don't receive a response we recommend looking at r/LearnPython or joining the Python Discord server at https://discord.gg/python where you stand a better chance of receiving a response.

/r/Python
https://redd.it/13jlfti
Easiest way to add write-through cache for authuser queries?

Hello! I'm new to django. I have an existing app with quite a lot of scale, and I'm seeing that simple reads from the auth\
user table (ie SELECT .. WHERE id=$1) account for a huge amount of load on database. I'd like to put a write-through cache in front of that. It's very read heavy so I think it should effectively reduce that load to near-zero.

What's the easiest path to do this? Is there any way to override just the queries for the select and the insert/update? I've tried googling, but there are a lot of similar topics that make it hard to hone in on the best path, so I thought I'd bring it to y'all :-)

/r/django
https://redd.it/13jbzs1
Flask hosting for specific purpose

Hello. I am sorry if this comes off as an oblivious, and perhaps frequently asked, question. I am quite new to this, and am overwhelmed with the different options.

For my bachelor project, I will need to post a specialized survey, which I have created as a flask application, containing the survey questions, as well as some background code to show randomized context each time the survey is taken, and then storing the survey responses.

Right now, I simply have the server hosted on a free Repl.it server, but as the application will need to properly manage upwards of 300-500 concurrent requests at a time, I will need to host it on a more capable server to avoid latency problems/errors, but that allows for the survey responses to be stored and continuously updated, either in a dedicated database, or simply writing them into a JSON file or something like that.

As such, I am looking for the best/simplest place to host this application properly. I won't expect this to be entirely free, but am hoping that it is not too expensive, especially since the website only needs to be up for a few weeks.

/r/flask
https://redd.it/13jdslm
Issues with form submission and form.validate_on_submit()

I am having issues with a form submission using wtforms and form.validate_on_submit(). Here is the code from the following files:

myapp/forms.py:

from flask\_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired

class LoginForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
password = PasswordField('Password', validators=[DataRequired()])
remember_me = BooleanField('Remember Me')
submit = SubmitField('Sign In')



myapp/routes.py:

from flask import render_template, flash, redirect, url_for
from myapp import app
from myapp.forms import LoginForm

@app.route('/login', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('index'))

form = LoginForm()

if form.validate_on_submit():
flash("{}".format(form.username.data))
return redirect(url_for('index'))

flash("setup")
return render_template('login.html', form = form)




myapp/templates/login.html:

{% extends "clientbase.html" %}

{% block content %}
<main id="main">
<section id="breadcrumbs" class="breadcrumbs">
<div class="container">
<div class="d-flex justify-content-between align-items-center">
<h2>Login</h2>
<ol>
<li><a href="{{ url_for('index') }}">Home</a>
<li>Login</li>
</ol>
</div>
</div>
</section>
<section>
<div class="container">
<div class="row">
<div class="col-lg-11 col-md-4 text-lg-left">
<h3 data-aos="fade-up">Login</h3>
{%

/r/flask
https://redd.it/13jd01c
Design patterns with django?

There is some common design patterns used in django? Does the default folder structure scalable? It is possible to use some other like Hexagonal Structure with django?

/r/djangolearning
https://redd.it/13jhahf
Pyscan: A command-line tool to detect security issues in your python dependencies.

pyscan v0.1.0 | Github

+ blazingly fast and efficient scanner that can be used to scan large projects fairly quickly.
+ automatically uses requirements.txt, pyproject.toml or straight from the source code (though not reccomended)
+ easy to use, and can be integrated into existing build processes.
+ In its very early alpha stage, so some features may not work correctly. PRs and issue makers welcome.

## Install

pip install pyscan-rs


or

cargo install pyscan


or check out the releases.

## Usage

Go to your python source directory (or wherever you keep your requirements.txt/pyproject.toml) and run:

pyscan

or
pyscan -d path/to/src


that should get the thing going.
Here's the order of precedence for a "source" file:

+ requirements.txt
+ pyproject.toml
+ your python source code (.py) highly not reccomended

Any dependencies without a specified version defaults to its latest stable version. Make sure you version-ize your requirements and use proper pep-508 syntax.

/r/Python
https://redd.it/13jq6bw
Tips Multicontainer Deployment on AWS or other Cloud Provider

Hi all, the company I‘m working on is deploying a multi-container application (Django, Redis, Nginx) to AWS Elastic Beanstalk with a docker-compose file. The deployment takes forever and is quite error prone. Any tips on deploying such kind of apps to AWS in an easy and reliable way? It would also be possible to change the cloud provider, if there is a more developer friendly way. Thanks!

/r/django
https://redd.it/13jw01v
Reverse django migrations

There is a way to reverse a migrations in django? i've seen this feature in other frameworks but i have never listen to something like that in django. I always forget to put an attribute in models and i need to delete or modify in the database, especially when it is the uuid

/r/djangolearning
https://redd.it/13kault