pythondaily | Education

Telegram-канал pythondaily - Python Daily

1102

Daily Python News Question, Tips and Tricks, Best Practices on Python Programming Language Find more reddit channels over at @r_channels

Subscribe to a channel

Python Daily

Error Appears After User Idles for A While (Assuming a Postgresql DB Connection Timeout) How can I handle this gracefully? Plus a ton of other questions.

`django.db.utils.OperationalError: consuming input failed: server closed the connection unexpectedly`

`This probably means the server terminated abnormally`

`before or while processing the request.`

`server closed the connection unexpectedly`

`This probably means the server terminated abnormally`

`before or while processing the request.`

While developing my app getting it ready for launch I have noticed that if I have accidentally left my dev server running overnight I see this error in the terminal when I refresh my browser and I get a 500 Internal server error in the browser.

My App is hosted on Railway using:

whitenoise==6.7.0
psycopg==3.2.2
psycopg-binary==3.2.2
psycopg-pool==3.2.3
gunicorn==23.0.0

Procfile:

web: gunicorn project_name.wsgi --log-file
web: python manage.py migrate && gunicorn project_name.wsgi

I am assuming I just need to change my procfile config to the following to prevent these issues. Also adding an 'CONN\_MAX\_AGE = 0' update to my DATABASES settings should correct the issue yes?

New Procfile:

release: python manage.py migrate
web: gunicorn project_name.wsgi:application --bind 0.0.0.0:$PORT --workers 3 --threads 2 --timeout 120 --master --log-level info --access-logfile '-' --error-logfile '-'

DATABASES in settings.py:

DATABASES = {


/r/django
https://redd.it/1gacktw

Читать полностью…

Python Daily

I need help with Django REST JSON parse error

I am very lost with this issue.

The stack is React, Redux, Django REST framework, Gunicorn, Nginx all in Docker.

I am sending a simple POST request from the frontend, all it contains is this data:

let data = {reportid: selectedReportId}

I verified that selectedReportId is not null or undefined. Viewing the request via browser tools, I can see the request and everything looks fine, including the JSON data.

However, it returns a 400 Bad request.

The urlpatterns in
urls.py:

...
path('reports/generate/', views.generate
report, name='generatereport'),
...

The corresponding view:

@csrfprotect
@apiview(['POST'])
@permissionclasses(IsAuthenticated)
def generatereport(request):
data = JSONParser().parse(request)
report
id = data.get('reportid')

if report
id is None:
return JsonResponse({'Error': 'Submitted report ID does not exist'})
else:
report = getobjector404(Report, pk=reportid, user=request.user)


/r/django
https://redd.it/1ga8jwx

Читать полностью…

Python Daily

Django web app hosted locally

Hello, I am currently exploring Django because it has good security and my seniors suggested it. Currently they want me to use Django and have a super user and regular user. The super user can do CRUD (create, read. update and delete) data on the cloud/local data base. The regular user has a calendar dash board that has a search function and can search specific dates: Example: January 1, 2024 - it will then list down all the information of data from that specific date only.

My seniors are also pushing Mongo DB, both used for local for User:(Signup/Login) for local and another Mongo DB in cloud that is hosted either via AWS or Google providers of MongoDB.

Is this doable? and how will you tackle this if you are in my place? Thank you for suggestions/helps.

/r/django
https://redd.it/1ga4b54

Читать полностью…

Python Daily

How call a Django API from inside a Django process?

I have complex Django app with many different endpoints and pretty complicated filtering.

I want to build an export service that accepts the URL and query string. The makes a call and it pages thru the data writes to a file.

For example, say there is a User API:
"/api/users/?namestartswith=Bob"

Consider the database is larger and there are 100k "Bobs". I want to page thru this and write results to a file.
Finally, the query sets behind the API are sensitive to which user calls them. So the results are dependent on request.user.

For large exports, this has to run in background task. The question is how in the background test do I effectively fake the user auth. I can't just copy the user auth over as it may/will expire.

I could easily write the ORM again, but I really want to use the existing filters and validated business logic.

The only way I have come up so far is to use some of the test infrastructure like the RequestFactory to make the calls inside my task. We do this all the time in our tests.

Alternative is to make some auth feature that allows for user impersonation which seems dangerous.

/r/django
https://redd.it/1g9v4p4

Читать полностью…

Python Daily

D R LLMs frameworks for research

I'm a Ph.D. student in AI and NLP and I'm currently starting a new research project with LLMs.
This time, instead of writing all the code from scratch, primarily using HuggingFace and Pytorch, I'd like to use one of the popular frameworks (like LangChain, LlamaIndex etc.).
The motivation behind this is that, ideally, I'd like to learn to use these tools to get a more compact and organised codebase, such that I can easily add pieces to include RAG, Agentic workflows etc.
I'm also interested in having an efficient way to load models and make inferences.

In your experience, which of the many available frameworks out there is the most suitable for research purposes ? And do you even use a framework or you just code everything from scratch every time you start a new project ?

/r/MachineLearning
https://redd.it/1g9k0te

Читать полностью…

Python Daily

Pyloid: A Web-Based GUI Framwork for Desktop Applications - v0.14.2 Released

# 🌀 What is Pyloid?

Pyloid is the Python backend version of Electron and Tauri, designed to simplify desktop application development. This open-source project, built on QtWebEngine and PySide6, provides seamless integration with various Python features, making it easy to build powerful applications effortlessly.

# 🚀 Why Pyloid?

With Pyloid, you can leverage the full power of Python in your desktop applications. Its simplicity and flexibility make it the perfect choice for both beginners and experienced developers looking for a Python-focused alternative to Electron or Tauri. It is especially optimized for building AI-powered desktop applications.

# 🎯 Target Audience

Pyloid is ideal for:

Python Developers: Build desktop apps with Python without learning new languages like Rust or C++.
AI/ML Enthusiasts: Easily integrate AI models into desktop applications.
Web Developers: Leverage your HTML, CSS, and JavaScript skills for desktop app development.
Electron/Tauri Users: Enjoy a similar experience with enhanced Python integration.

# Key Features 🚀

Web-based GUI Generation: Easily build the UI for desktop applications using HTML, CSS, and JavaScript.
System Tray Icon Support
Multi-Window Management: Create and manage multiple windows effortlessly.
Bridge API between Python and JavaScript
Single Instance Application / Multi Instance Application Support: Supports both single and multi instance applications.
Comprehensive Desktop App Features:

/r/Python
https://redd.it/1g9hwas

Читать полностью…

Python Daily

Micro services with Django

Hi folks
I have done two big Django project they were monolithic

But i really like the idea of micro services but i was wondering is it worth the headache

Let's make this discussion about when micro services will be indicated
So

1 why

2 when

3 design cause i found people separate db or share it between multiple services



/r/django
https://redd.it/1g9et8z

Читать полностью…

Python Daily

A search engine for all your memes - written in Python.

The app is open source and written entirely in Python 👉 https://github.com/neonwatty/meme\_search

# What My Project Does

The open source engine indexes your memes by their visual content and text, making them easily searchable. Drag and drop recovered memes into any messager.

The backend is built using these great open source components:

moondream: a tiny, kickass vision language model
all-MiniLM-L6-v2: a popular text embedding model
faiss: a fast and efficient vector db
sqlite: the swiss army knife of dbs

# Target Audience

This is a toy project. Open source and made for fun.

# Comparison

immich: great open source image organizer, does not (yet) have powerful image-to-text indexing
other local photo apps: some allow for indexing but not quite at the level of a vlm yet

/r/Python
https://redd.it/1g93yyw

Читать полностью…

Python Daily

Having hard time implementing search functionality in django

I'm a complete beginner in backend development and I'm currently working on a search functionality that includes multiple advanced filters. I'm using Django Filters for filtering, but since I'm new to this, I haven't found many tutorials on YouTube. Most of the content I came across relates to Q objects, which raises the question of which is better to use: Django Filters or Q objects?

Additionally, I'm gathering data from APIs for weather, air quality, and datasets related to the cost of living. I'm unsure how to store this data so that when users apply filters in their searches, the system can display relevant cities. The project is aimed at digital nomads looking for cities based on specific criteria.

I would greatly apprciate any guidance on these topics, and I apologize if my questions seem basic.

/r/django
https://redd.it/1g98l21

Читать полностью…

Python Daily

Personal portfolio

I made my personal portfolio using flask, I am serving a blog and resource sharing there. Just wanted to show it to the world, theres a link to a flask ecommerce template there under resources if someone wants to take a look! Also feedback is welcome
silverboi.me https://silverboi.me

/r/flask
https://redd.it/1g95uh4

Читать полностью…

Python Daily

Can I migrate my website from WordPress to a coded platform like Django

Five months ago, I posted a similar question on the WordPress subreddit, but it didn’t gain much traction, and the feedback was mostly negative, tbh.

My original post was along these lines:

"I'm currently trying to launch an e-commerce site using WordPress with a theme builder. I really believe this business idea can succeed in the near future (God willing). This might sound odd, but I feel uneasy about WordPress, even though I’ve never written a single line of code. To add to that, I’m not comfortable using Dokan (the marketplace plugin) either, as I want to create a highly customized multi-vendor marketplace."

Fast forward a few months, and I've returned to working on my website. I’m not as uncomfortable with WordPress anymore, and I’ve completed the entire site design—it looks great! But now, I’m aiming Big. I want to grow this into a fully-fledged platform with apps for buyers, sellers, and delivery. I’m considering finding a technical co-founder to build this vision from scratch. The challenge? My budget is tight (hence, WordPress initially).

Any advice on how to proceed or whether I should stick with WordPress? Should I really be looking for a co-founder at this stage?

/r/django
https://redd.it/1g8z6h0

Читать полностью…

Python Daily

Best framework for large scale web apps

Wondering what’s the community’s best web framework for building large scale and reliable web apps ? Especially django (with rest framework) VS flask VS fastapi

/r/Python
https://redd.it/1g8xeoy

Читать полностью…

Python Daily

How long can you confidently build app using django from Flask experience?

recently learn web development using flask, started 3 months ago but not consistent, building a capstone project.
I notice that django jobs are in demand than flask.

Can you share the learning curve switching frameworks.?

/r/flask
https://redd.it/1g8uiwl

Читать полностью…

Python Daily

temporals - A time, date and datetime periods support

Hi guys!


I'm excited (and mostly nervous) to share my first Python project that is aimed at the wider audience of Python users.


# What Does It Do


temporals aims to provide a minimalistic utility layer on top of the Python standard library's datetime package in regards to working with time, date and datetime periods.


The library offers three different flavours of periods:

`TimePeriod`
DatePeriod
`DatetimePeriod`


As you can probably guess from the names, each period corresponds to a `datetime`object and contains the same as its start and end variables.


When working with periods, each and every one of them is viewed as existing on a timeline, thus each period contains its own implementation of methods related to that position in time:


Methods like is_before, is_after and get_interim serve the purpose of comparison between periods that exist in time in a non-overlapping fashion;
Methods like `overlapped_by`, `overlaps_with`, `get_overlap` and `get_disconnect` provide the ability to work with periods that exist in time in an overlapping fashion;
Operations such as equality between periods and the in keyword allow you to determine if a period is equal to, or exists fully within, another period


Additionally, each period instance contains a Duration under its duration attribute which is built on top

/r/Python
https://redd.it/1g8nu9s

Читать полностью…

Python Daily

D Efficient CNNs for inference

I am working on an object detection project using high resolution images.

Are there any techniques that can make a trained CNN (UNet) efficient during inference? I know pruning is one such technique, but it risks loss of accuracy and parallelizability.

/r/MachineLearning
https://redd.it/1g8kpl6

Читать полностью…

Python Daily

How do I query a column in a custom wtf flask validator when using flask-ckeditor?

If I have a column in the db table with current_title = <p>A1</p> how do I query it in in flask and flask sqlalchemy.

In a flask wtf custom validator I tried
post_db = db.session.execute(db.select(Posts).filter_by(title=current_title)).scalar_one_or_none() but post_db is always None.


I know this because I went flash(post_db). I am 100% of the value because I checked dbeaver. Also the reason I am getting <p>A1</p> is because I am assume I am using flask ckeditor.


Though when I looked at dbeaver the text column is https://imgur.com/a/fDBobqZ. Notice the weird character at the end.
Here is the relevant code.

class Posts(FlaskForm):

title = CKEditorField('title', validators=[DataRequired('text is required'
submit = SubmitField('Submit')]

class Book(db.Model):
id: Mapped[int] = mapped_column(Integer, primary_key=True)
text: Mapped[Optional[str]] = mapped_column(Text(), unique=True)





/r/flask
https://redd.it/1g9omd5

Читать полностью…

Python Daily

rending html file just get a blank page

as the title says every time, I open up my webpage I just get blank page despite my html file having content.

yes, I have it in a separate folder called templates, no my .py is not in the templates folder. they are connected to the same route i checked. and yes, I spelled it correctly when return render_template("index.html")


i dont know if this helps but im using vscode text editor.



/r/flask
https://redd.it/1ga13ew

Читать полностью…

Python Daily

Am I doing it wrong for only using HTML and vanilla JS?

I've made webapps with VueJS before but it struggled with SEO. For the SEO part of that project I had to create a separate Flask-rendered page and import Vue as components on the page. I have not learned Nuxt yet.

Recently I tried Alpine.js for one project and then vanilla JS for another. I find it's easier to just use vanilla JS for the most part. Maybe because I'm relatively new to JS so I felt the vanilla JS is easier to grasp than frameworks. Granted these are very simple apps maybe that's also the reason. Am I missing something for not using a framework like Vue for simple apps? Does anyone use vanilla JS for a medium-complexity app?



/r/flask
https://redd.it/1g9p3o7

Читать полностью…

Python Daily

How to run science projects

I’ve put together my experience for running ML & science projects based on 9+ years at a FAANG company. It covers the usual stuff like figuring out vague business problems, finding the right stakeholders, setting up metrics, and getting things done. I also share some personal stories about what’s worked (and what hasn’t), especially when stakeholders aren't on the same page. If you’ve done similar work or have different approaches, I’d love to hear what you think!

https://dzidas.com/ml/2024/10/22/implementing-data-science-projects/

/r/MachineLearning
https://redd.it/1g9kakg

Читать полностью…

Python Daily

From jupyter to vscode

Hi everyone, I’ve been using Jupyter notebooks for a while, but as my files grow larger, maintaining them has become cumbersome. I’d like to switch to VSCode to run selected code for testing, but every time I do, I have to rerun the entire code. How do machine learning engineers or data analysts handle large notebook files efficiently?

/r/Python
https://redd.it/1g9p2tj

Читать полностью…

Python Daily

is django still relevant in 2024?



/r/djangolearning
https://redd.it/1g7u5on

Читать полностью…

Python Daily

Guidance on deployment

Hi guys!!
So, I have a flask application that I want to deploy over the internet for data transmission between a webhook application and a computer doing a bunch of scientific calculations.

The idea is to deploy this app to be visible over the internet, so I can send post request (from the webhook) to the flask application, the flask application request the calculation from the machine, and send it back.

So, I'm wondering if I need some special configuration to protect the network or it should be fine just with flask. (Note such service will not be available for anyone but the backend function with the IP address and port of the machine that request the calculations).

Also, the machine running the post get request should the same one as the one running the scientific calculations or in the same local network.

Can anyone point out the risk of such idea (vulnerability on the system) or any other recommendations?

/r/flask
https://redd.it/1g9iyc3

Читать полностью…

Python Daily

Tuesday Daily Thread: Advanced questions

# Weekly Wednesday Thread: Advanced Questions 🐍

Dive deep into Python with our Advanced Questions thread! This space is reserved for questions about more advanced Python topics, frameworks, and best practices.

## How it Works:

1. **Ask Away**: Post your advanced Python questions here.
2. **Expert Insights**: Get answers from experienced developers.
3. **Resource Pool**: Share or discover tutorials, articles, and tips.

## Guidelines:

* This thread is for **advanced questions only**. Beginner questions are welcome in our [Daily Beginner Thread](#daily-beginner-thread-link) every Thursday.
* Questions that are not advanced may be removed and redirected to the appropriate thread.

## Recommended Resources:

* If you don't receive a response, consider exploring r/LearnPython or join the [Python Discord Server](https://discord.gg/python) for quicker assistance.

## Example Questions:

1. **How can you implement a custom memory allocator in Python?**
2. **What are the best practices for optimizing Cython code for heavy numerical computations?**
3. **How do you set up a multi-threaded architecture using Python's Global Interpreter Lock (GIL)?**
4. **Can you explain the intricacies of metaclasses and how they influence object-oriented design in Python?**
5. **How would you go about implementing a distributed task queue using Celery and RabbitMQ?**
6. **What are some advanced use-cases for Python's decorators?**
7. **How can you achieve real-time data streaming in Python with WebSockets?**
8. **What are the

/r/Python
https://redd.it/1g94p5f

Читать полностью…

Python Daily

Where i can learn docker

Hi i need to learn docker give me some resources

/r/djangolearning
https://redd.it/1g7kenb

Читать полностью…

Python Daily

Configuring a Lambda to act as a Celery Worker.

Hi, i'm looking for advise on how to proper use a lambda to mimic a worker job, by using the following configuration:

AWS MQ using rabbitMQ as broker.

Lambda with configured Trigger listening on a MQ queue.

A Workflow defined in my celery app with some tasks:

task\_01: runs on a celery worker instance.
task\_02: runs on lambda, i put then on a specific queue that no other worker listen to.
task\_03: runs on a celery worker instance.

    workflow = chain(
        task_01.s(),
        task_02.s(),
        task_03.s()
)

If i execute task\_01, the lambda on task\_02 is triggered correctly, but i need to mimic celery code, save the result to the backend and trigger task\_03 correctly.

I'm currently using the code below to save the task\_02 result, but i need to make some workflow functions like chain, group, chord, to be called.

from celery.worker.request import Request

def handler(event, context):
message_data = # get the data from the

/r/django
https://redd.it/1g97dfl

Читать полностью…

Python Daily

AI-generated pictures: What do you use?

Hey guys, I'm searching for some tool/website/anything that could take my design of the Website and generate pictures similar to my branded website.

Something like train model with pictures of my website and then generate new pictures with the same fonts and colors.

Is there any simple way to do so? Thanks!

/r/flask
https://redd.it/1g8oayh

Читать полностью…

Python Daily

Issues with flask, uwsgi, nginx, and websockets.

I have an existing application (resume website) that is being served just fine with flask/python/uwsgi/nginx, the problem was when I tried to introduce websockets. I have read the documentation for flask socket.io in terms of deployment, not even sure if I need it, honestly I have no idea what the h3ll I am doing with the sockets. It seems like there is gevent, websocket native uwsgi, socket.io, and probably a myriad of others.

I essentially wrote a chat server backend in golang that uses rabbitmq, and i then wrote a test python script that sends messages over websockets using asyncio and websockets, this works fine.

The thought behind the project was to have an administration page be served by flask that allows the admin to see all rabbitmq queues, choose a queue, read messages from queue, and respond to queue. I could achieve this through polling directly to the backend, but I wanted to securely incoporate websockets to handle this in a secure asynchronous way being served by flask/python.

Then implement a chat modal on my resume page that allows anonymous users, to initiate chats, creating a rabbitmq queue unique to that user based on session id, admin receives new queues and messages,

/r/flask
https://redd.it/1g8zmh0

Читать полностью…

Python Daily

Asynchronous framework for working with RabbitMQ

Hello everybody! I wrote the first version of a small (so far) framework that allows you to work with the RabbitMQ broker.

I hope that this small project can benefit people and grow into something more. The main idea is to process essays asynchronously without resorting to extending the code ring in the project. Among the nice bonuses: there is validation of field types from messages (something like what is in FastAPI, only without dependency injection).

I would like you to study my code and, if anyone has any criticism or recommendations, leave your feedback. Thank you very much!

https://pypi.org/project/aiocarrot/
https://github.com/d3nbr0/aiocarrot/

/r/Python
https://redd.it/1g8dt16

Читать полностью…

Python Daily

What do you do when you are frustrated?

Hi everyone!

I know this post deviates a bit from what people usually post here, but I thought it would be interesting to hear what those of you who are already developers or have code longer do with this.

How do you deal with the frustration? I'm in the phase where I haven't fully understood yet that writing code and making it actually work properly takes time, so I can get frustrated when I've been sitting for a little while and haven't found a solution yet.

Of course, what I'm doing now is just stepping away from the PC for a while and thinking about something else, but want to hear what other tips you have.

Thank you!

/r/Python
https://redd.it/1g8jrmd

Читать полностью…

Python Daily

Live server set-up for developing with Flask?

I'm new to programming (few months in, learning in my spare time) and I've started to learn Flask to develop web apps with Python.

I'm using VSCode mostly and what I really need is for a live server type of setup so I can see the changes I'm making in HTML/CSS, as I make them. The difficulty I'm facing is that the live server plugins I've tried in VSCode don't show the results properly. E.g. they will display {{ myflaskvariable }} rather than the actual variable - which obviously makes things harder than I feel they need to be!

Everything displays fine using 'flask run' from the terminal, but I can't seem to get a live server to work the same way. Any suggestions?

/r/flask
https://redd.it/1g8lchg

Читать полностью…
Subscribe to a channel