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
I’ve just deployed my Flask website in pythonanywhere.com

http://rambler.pythonanywhere.com/
Appreciate any suggestions

/r/flask
https://redd.it/13axqjf
Test On 4 Concurrent Jobs Using Python-Polars 0.17.11 to GroupBy Billion Rows

Python 3.11.2 was able to run big data jobs concurrently using limited memory. My testing script with 14 columns and a data file size of 67.2GB was completed in less than 10 minutes using 32GB Memory and 8-Core. This is strong evidence that Python is capable of handling big data jobs with limited resources.

Script
import polars as pl
import time
import pathlib
s = time.time()


q = (
   pl.scan_csv("Input/1000MillionRows.csv")    
   .groupby(by=["Ledger", "Account", "PartNo", "Contact","Project","Unit Code", "D/C","Currency"\])
  .agg([  
    pl.count('Quantity').alias('Quantity(Count)'),
    pl.max('Quantity').alias('Quantity(Max)'),
    pl.min('Quantity').alias('Quantity(Min)'),
    pl.sum('Quantity').alias('Quantity(Sum)'),     
    pl.sum('Base Amount').alias('Base Amount(Sum)'),     
  \]))


a = q.collect(streaming=True)
path: pathlib.Path = "Output/Polars-GroupBy.csv"
a.write_csv(path)


e = time.time()
print("Polars GroupBy 1000 Million Rows Time = {}".format(e-s))

​

Demo video in 10X fast forward: https://youtu.be/odDOlU9KNqY

Without fast forward: https://youtu.be/Ze0jNmtUn0Y

/r/Python
https://redd.it/13bb59x
Flask mail sending duplicate emails

I have a flask app that I've implemented a simple 2fa feature on. The user is emailed when they arrive at the route requiring them to enter the code sent to their inbox.

Unfortunately it seems like the app is sending more than one email to the user. Sometimes 2, sometimes 3 emails. I can't seem to figure out why.

Here is my email code, it (send2faemail) is only being called once in the route:

def send_2fa_email(email, x):
msg4 = Message('Security Code', sender='no_reply@some.app', recipients=[email])
msg4.body = f'''Your username is being used to login to SomeApp. Your multi-factor authentication code is:
{x}

This code will expire in 3 minutes. If you do not complete login in that time, you will need to start the login process again.

If you did not make this request, you can ignore this email. If you believe someone is attempting to access your account without your consent, please notify us at support@some.app.
'''
mail.send(msg4)


Has anyone encountered this before? We're using AWS SES for email but haven't had this issue previously.

/r/flask
https://redd.it/13bojqk
Starfyre - A Python Web Framework for creating frontend web applications

Hey Everyone! 👋
Over the past two months, I've been hard at work developing a new Python frontend web framework, and I'm excited to announce its first minimal release: Starfyre.
Starfyre is a Python web framework designed to simplify front-end web application development. Starfyre offers a user-friendly and powerful solution for crafting dynamic web applications by seamlessly bridging back-end and front-end development in the Python ecosystem. By unlocking untapped potential in Python front-end development, Starfyre empowers developers to create engaging and interactive applications easily.

Some of the key features are:
\- Single-file reactive components
\- Built-in state management
\- Server-side rendering
\- PyML, a custom JSX-like language
\- Support for both client-side and server-side Python
\- Integrated CSS and HTML support
\- Ability to write JavaScript if need be
\- Familiar syntax and easy learning curve

You can check out the project at https://github.com/sansyrox/starfyre
I have also created a blog to explain the future visions - https://sanskar.wtf/posts/hello-starfyre
Most importantly, you can find an example app on GitHub(https://github.com/sansyrox/first-starfyre-app).
Feel free to share your thoughts and suggestions! I'm all ears and can't wait to hear what you all think! 😄

/r/Python
https://redd.it/13bloxf
Advanced Django books

Hi to everyone!. I would like to ask you if you can tell me some books with advanced django subjects as Cache, Inheritance (advanced), monitoring app, models, etc...?

thanks!

/r/django
https://redd.it/13bofj2
DRF - Limiting fields based on view

I'm beginning to dive into DRF and I've got a question concerning serializers. It's straightforward enough to bash out a serializer for a model, but I'm trying to wrap my head around how to limit the data made available at different levels in the API.

For instance, take a simple widgets model with 3 fields: field_a, field_b, and field_c. Let's say that at the top of the api (/api/widgets) I want to list each of the widgets but only return field_a. If I specify the exact widget (/api/widgets/1), it returns more detail about that specific widget (field_a, field_b, and field_c).

On the surface, it appears as though I would need to write a serializer for each of these instances, or at least subclass one from the other changing the fields as needed. Is this the case?

/r/django
https://redd.it/13c1ghc
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/13c9b4a
Affordable Hosting?

Hi everyone, I know this is probably asked all the time but I am really struggling to find an affordable hosting engine for my flask application. The main reason being that my application is quite ram intensive. It is a game recommendation engine for my web application and the way it works is that it loads a fairly large .pkl file into memory that contains pre-computed similarity scores for the games. This file is about 6.7gb in size and when first launching the app the ram usage skyrockets until the file has loaded at which point it drops to around 2-3gb.

I am a college student and this is for my final year project, all the sources I have seen online cost at least 50$ a month for what I think I need, and I really cannot afford something like that. Any idea what I should do?

/r/flask
https://redd.it/13c3y3i
Handling Bulk uploads via csv

I'm looking to expand/streamline some basic functionality in my flask app.


Currently, a registered user can submit a form that does the following:


1: Looks to see if a row exists based on email, if not, create a new database entry 'users'

2: Creates a new database entry 'transaction' (tied to users)

3: Creates 4 new database entries 'emails' (tied to transaction)



Pretty simple, works as intended.


But, the next evolution of this is allowing users to bulk upload via a csv. the csv would mirror exactly the flask form, so it would be doing precisely what is happening above, except at a much much larger scale. (instead of 1 at a time, possibly hundreds)


Is this something that requires a task scheduler like celery or risk exploding my database/server?


From what I've read, celery is a burden to not only initialize but also maintain all its own so while I'm a team of one I'm trying to keep things as lightweight as possible.


Any high level advice/pointers is greatly appreciated!

/r/flask
https://redd.it/13bpkj1
Using flask-session with redis-sentinel in k8s, redis master dies, app dies.

When testing my app when the redis master pod gets deleted (there are two slave pods, three total), it throws a 500 error since i can't reach the dead master (timeout). Then, throws an error for not being able to write, obviously because the old master is now a slave. So, redis-sentinel is working correctly.

I tried beforerequest and afterrequest functions to update the app.config'SESSION_REDIS' details but that gets loaded when flask starts.

I am trying to avoid any outages when upgrading or restarting redis pods.

/r/flask
https://redd.it/13bphft
problem no such table after upgrading

Hi all,

i have upgraded my system to the latest ubuntu server lts, and after upgrading i have the problem that flask_sqlalchemy seems just not to open the db anymore.

\- the db exist- i have checked with sqlite3 in the console that everything exists.

my config is the following:

app.config'SECRET_KEY' = 'mysecret'
app.config"SQLALCHEMY_DATABASE_URI" = 'sqlite:///verein.sqlite'
db = SQLAlchemy(app)

if i tried to open the app, it crashed with:

File "/usr/local/lib/python3.10/dist-packages/sqlalchemy/engine/default.py", line 920, in doexecute
cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: buchungen
[SQL: SELECT
buchungen.id AS buchungenid, buchungen."desc" AS buchungendesc, buchungen.note AS buchungennote, buchungen.category AS buchungencategory, buchungen.amount AS buchungenamount, buchungen.date AS buchungendate, buchungen.account AS buchungenaccount, buchungen.deleted AS buchungendeleted
FROM buchungen]

After backupping my database i added db\
create_all(), just once and removed ita again.

now it works, but without any of the present data.

It just seems it just not uses the specified file, because as said before the file is now in place and all data is available.

Any ideas ?

Thank you very much.

/r/flask
https://redd.it/13bjq8v
After tearing my hair out writing JavaScript the last few days how close are we to Python in the browser?

Can someone point me to any recent articles and news on true back end and front end use of Python using HTML and CSS on front-end, with Python in place of JavaScript, presumably using WASM/WASI ?


I know about pyscript but not sure if this is the answer (or perhaps it is)?


I basically never want to touch JavaScript again lol.

/r/Python
https://redd.it/13ccenx
Need help with Flask, SQLAlchemy and two identical Databases

Hello,

I have a flask app that's working so far as intended, however I need to extend its capabilities.

Currently it pulls data from one pgsql database and generates reports, but I need to bind a second, identically structured pgsql server.

I'm familiar with

app.config'SQLALCHEMY_BINDS' = { bindkey: connectionparams, etc..}

and I already have several databases from one MySQL server and one db from the aforementioned pgsql server configured.

The issue is that I need to access data from another pgsql server that's structurally identical to the first one. Same database name, same table structure, everything but the data is the same.

I can't dynamically change the __bind_key__ in the model class, so I thought I'd duplicate the model classes with a new __bind_key__, but that fails too because the __table_name__ values are the same. Error: "sqlalchemy.exc.InvalidRequestError: Table <tablename> is already defined for this MetaData instance. Specify 'extend_existing=True' to redefine options and columns...."

When I specify 'extend_existing=True', the new definition overwrites the old one, so that using the old classes uses the new __bind_key__ value, so that method is apparently out as well.

I cannot change the table names in the pgsql servers. They are appliances and it would severely break things if I went

/r/flask
https://redd.it/13claur
Redirecting to the correct page after registration with flask-security-too / flask-login?

I have a web app that allows users to send connections to other people to build out a network.

I'm using flask-security-too, and therefore flask-login under the hood.

I've got all the code working to generate the request and send the request via email to the target person, but it is entirely possible that the targeted individual does not have an account on the platform.

At the moment, when the invitation link is clicked on in the email it takes them to a specific view (connect/accept/<hashed key>) that does some sanity checking, provides an overview of what it means to accept the link, and the gives them the offer to accept or reject the request.

The code to process the acceptance is behind @auth_required, because I want them to either log in or sign up, but I can't seem to get the redirect to follow the right pattern - it always redirects to / after login or registration, and the acceptance never gets processed.

I'd expect it to go something like this:

Accept request
Log in or register if not logged in already
Return to processing view to update the request in the database
Redirect to dashboard

I've looked through the docs but I can't

/r/flask
https://redd.it/13bity2
How to register group models as groups in admin page?



I have been struggling heavily with the models of an e-commerce project that deals with different types of users, customers and vendors due to an utter lack of resources online on the topic of working with 2 or more groups using group models.

Because I had to map foreignkey relations between them and several other models I decided to create group models for customers and vendors. Due to lack of resources on the topic I am not even sure if what I ended up with was correct and even assuming it is, I just can't register the group models on admin page as groups. I could just create groups on admin page but then I don't know how to link that with the group model. I have honestly no idea how to proceed, please help on how to do it or if there's a less confusing way for this.

/r/djangolearning
https://redd.it/13ch6z5
Invalid Argument on Passing refresh_token as parameter on Google OAuth Secret Manager Tutorial - "The provided Secret ID [] does not match the expected format [[a-zA-Z_0-9]+]"

Hello, I'm following a tutorial here: [https://www.youtube.com/watch?v=-LXrLVPmlfI](https://www.youtube.com/watch?v=-LXrLVPmlfI). I've been stuck on this issue for almost a month. This program is attempting to log-in to a Flask app using the Google OAuth and Secret manager. In the code, [auth.py](https://auth.py/) is sending a refresh\_token to [secret.py](https://secret.py/), which is supposed to create a secret. Alot of this code is taken directly from Google documentation, and the tutorial was released by Google. The relevant pieces of code are as follows: auth.py

def oauth2callback(passthrough_val, state, code, token):
if passthrough_val != state:
message = "State token does not match the expected state."
raise ValueError(message)
flow = Flow.from_client_secrets_file(_CLIENT_SECRETS_PATH, scopes=[_SCOPE])
flow.redirect_uri = _REDIRECT_URI
# Pass the code back into the OAuth module to get a refresh token.
flow.fetch_token(code=code)
refresh_token = flow.credentials.refresh_token
secret = Secret(token)
secret.create_secret_version(refresh_token)

secret.py



/r/flask
https://redd.it/13cvs9g
abacus - minimal accounting framework

Hello everyone, I wrote a small library that can create a chart af accounts, process accounting entries, and produce balance sheet and income statement. I recently added "conta accounts" (eg depreciation) which make the library conceptually fit as a working double entry general ledger.

My initial interest was to make a proof of concept that a compact accounting library is possible, and now I'm at a point where I need to find rationale for it's development. So far it is a research demo, not a accounting software. Maybe useful for teaching code to accountant or accounting to programmers, but these seem very distant professions (eg thematic LinkindIn group on accounting and programming has 10 members).

While writing the code I really enjoyed pattern matching that helps a lot to branch code, as well as subclasses to distinguish between types of accounts. I was able to show the workflow Chart -> Ledger -> ListEntry -> Ledger -> Report generally works for accounting and one can save and process a list Entries in order to be able to get the state of the Ledger. Anything stateful was causing concern and needed extra handling (eg closing entries).

Will appreciate your comment about

/r/Python
https://redd.it/13cmb21