pythondaily | Образование

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

Подписаться на канал

Python Daily

jinja template rendering incorrect

Anyone know why jinja template statements would get borked while being rendered?

I have a couple of if statements in my config ie:

\## template: jinja
{% if distro == 'rocky' or distro == 'centos' or distro == 'ol' or distro == 'almalinux' or distro == 'fedora' or distro == 'redhat' %}



Is rendered as:
{0 if distro == 'rocky' or distro == 'centos' or distro == 'ol' or distro == 'almalinux' or distro == 'fedora' or distro == 'redhat' 0}

I do not know what process renders the jinja code, it was working then one day I started noticing all {% show up as {0 and making it where jinja can't process the variables.
Thanks

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

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

Python Daily

The amazing architect strikes

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

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

Python Daily

Hatch or uv for a new project?

I'm starting a new project, and I can't decide if hatch is needed anymore.
Hatch can install packages using uv for speed, so I used both before.

But uv allows monorepo, while hatch doesn't support it yet.

What are the differences between the two tools ? What would you choose for a new project?

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

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

Python Daily

Help with Creating a Seat Model Linked to Backend in Bus Booking App


Hi all,

I’m working on a Django project for a bus booking system. I’ve already created the Agency and Bus models, but I’m stuck on how to implement the seat structure. Here’s what I’m aiming for:

• I want to create a Seat model that is linked to a Bus, which in turn is associated with an Agency.
• On the frontend, I want to display the seats as they appear in an actual bus (for example, in a 2x2 seating arrangement or other layouts).
• The seats need to be generated dynamically based on the bus assigned.

Could someone guide me on the best way to structure the Seat model and how to display the seats in the view? Any help on connecting these models and ensuring the seats are linked correctly to each bus would be appreciated!

Thanks in advance!

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

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

Python Daily

Project World's first autonomous AI-discovered 0-day vulnerabilities

I'm sure a lot of people have found 0-day vulnerabilities by pasting code snippets into ChatGPT. The problem has always been scanning an entire project for 0-days. Some papers have shown it's possible by feeding their agents known vulnerable code, but as far as I know, none of those papers ever got any CVEs or found real 0-days. Vulnhuntr was released this weekend with more than a dozen 0-days discovered in open source projects of 10k+ GitHub stars:

https://github.com/protectai/vulnhuntr

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

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

Python Daily

Django admin not displaying registered LogEntry model

Hello, I've been attempting to display the Django built-in model LogEntry in the Django admin page, but it hasn't been successful. I'm using Django version 4.2.9. While my custom models are displaying correctly, the LogEntry model remains absent from the admin panel.

Here's what I've checked so far:

- Confirmed that django.contrib.admin is included in INSTALLEDAPPS
- Ensured the model is registered correctly within the appropriate app
- Even tried registering the model in a completely new (empty) app, which I added to INSTALLED
APPS, but it still did not appear.

Do you have any suggestions on why this specific model isn't appearing? What else should I consider checking?


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

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

Python Daily

Wednesday Daily Thread: Beginner questions

# Weekly Thread: Beginner Questions 🐍

Welcome to our Beginner Questions thread! Whether you're new to Python or just looking to clarify some basics, this is the thread for you.

## How it Works:

1. Ask Anything: Feel free to ask any Python-related question. There are no bad questions here!
2. Community Support: Get answers and advice from the community.
3. Resource Sharing: Discover tutorials, articles, and beginner-friendly resources.

## Guidelines:

This thread is specifically for beginner questions. For more advanced queries, check out our [Advanced Questions Thread](#advanced-questions-thread-link).

## Recommended Resources:

If you don't receive a response, consider exploring r/LearnPython or join the Python Discord Server for quicker assistance.

## Example Questions:

1. What is the difference between a list and a tuple?
2. How do I read a CSV file in Python?
3. What are Python decorators and how do I use them?
4. How do I install a Python package using pip?
5. What is a virtual environment and why should I use one?

Let's help each other learn Python! 🌟

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

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

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

REST API Testing - Where to run functional tests?

Hello,

At work, I'm developing a REST API with flask, and I'm wondering where I should run my functional tests.

My current CI/CD pipeline on github actions runs the unit tests (mocking / patching ) for small blocks of code, and also runs the integration tests using a docker compose (spins up database instance, and some other services on the same machine, then runs tests)

Both those tests above run locally, and run during push and merges to github.

I also have functional testing for the REST API, using pytest with request library. Once the docker image is running in a test environment on AWS (EC2), I want to be able to make calls and check if the API is working properly

My question is where should these be run from and what do most professional organizations do? Should they run:

\- Locally? (i.e. my local computer send the request to the live test server on AWS)

\- On the Github action CI/CD pipeline? if so can i configure these to run after the docker image is running?

\- On the EC2 instance itself (maybe in another container)

\- Somewhere else?

Thanks for any advice!

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

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

Python Daily

cstructpy- Python package designed for binary serialization and deserialization of structured data

This app is open source and is made using Python: https://github.com/Maxim-Mushizky/cstructpy

Also can pip install it via pypi https://pypi.org/project/cstructpy/

# What My Project Does

Provides a simple interface for packing and unpacking binary data based on field definitions using Python's struct module. The motivation for this package is to have a data validation using type annotations, similar to pydantic but for binary data. Therefore this package is best when used alongside pydantic.BaseModel or dataclasses.dataclass since it allows a similar class structure and object creation

# Target Audience

Mostly effective for distributed systems, where there are c like structs passed as binary and need to be verified in different parts of the code.

# Comparison

There are no current comparisons, since this project attempt to mimic the object creation and validation as present in Pydantic

# Contribution

If you require any new features or have a use for this package you are more than welcome to join as a celebrator

Enjoy!

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

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

Python Daily

Anyone here going to PyCon Australia next month?

I'll be going for the first time and I'm keen to meet up with fellow Aussie Pythonistas! Anyone interested?

There's a lot of interesting talks planned - are there any in particular you're keen for?

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

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

Python Daily

Monoranger: A poetry plugin for managing monorepos

🔗 Link - [https://github.com/ag14774/poetry-monoranger-plugin](https://github.com/ag14774/poetry-monoranger-plugin)

# What my project does

I recently needed to set up a monorepo and I wanted to have similar structure as in Cargo workspaces from Rust.

Essentially I wanted to have a single lockfile shared between all components/projects in the library and have a single venv with all dependencies installed as this makes development so much easier. I use Poetry so I decided to create a plugin for this usecase.

This plugin:

* Enables multiple projects in a monorepo using Poetry to use a shared poetry lockfile
* Enables multiple projects in a monorepo using Poetry to use a shared virtual environment
* Allows projects to use path dependencies and pin their versions during 'poetry build'

# Target Audience

Any Python developer that wants to create a simple monorepo using Poetry and does not want to resort to complex build tools such as Nx. I will be keeping this plugin up to date and make sure that updates and fixes are pushed in a timely manner

# Comparison

The existing tools fall in two categories:

a) Compicated build tools that have their focus on other programming languages but also support Python. An example of this is Nx. It's a nice tool but it has too many features

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

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

Python Daily

Thursday Daily Thread: Python Careers, Courses, and Furthering Education!

# Weekly Thread: Professional Use, Jobs, and Education 🏢

Welcome to this week's discussion on Python in the professional world! This is your spot to talk about job hunting, career growth, and educational resources in Python. Please note, this thread is not for recruitment.

---

## How it Works:

1. Career Talk: Discuss using Python in your job, or the job market for Python roles.
2. Education Q&A: Ask or answer questions about Python courses, certifications, and educational resources.
3. Workplace Chat: Share your experiences, challenges, or success stories about using Python professionally.

---

## Guidelines:

- This thread is not for recruitment. For job postings, please see r/PythonJobs or the recruitment thread in the sidebar.
- Keep discussions relevant to Python in the professional and educational context.

---

## Example Topics:

1. Career Paths: What kinds of roles are out there for Python developers?
2. Certifications: Are Python certifications worth it?
3. Course Recommendations: Any good advanced Python courses to recommend?
4. Workplace Tools: What Python libraries are indispensable in your professional work?
5. Interview Tips: What types of Python questions are commonly asked in interviews?

---

Let's help each other grow in our careers and education. Happy discussing! 🌟

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

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

Python Daily

Talks from PyData Amsterdam 2024 now on YouTube

PyData has uploaded the talks from PyData Amsterdam 2024 (18-20 Sept) to YouTube.

- https://www.youtube.com/playlist?list=PLGVZCDnMOq0reU2lzNZCn9obkyRVaSnpF
- PyDataTV/playlists" rel="nofollow">https://www.youtube.com/@PyDataTV/playlists
- PyDataTV/videos" rel="nofollow">https://www.youtube.com/@PyDataTV/videos

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

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

Python Daily

I want to hide the DRF API views in my production code.

I have built a full stack mobile-web application using Flutter and Svelte with Django as the backend. All of the mentioned codes have been pushed to production. All of them function on the Django rest framework APIs(GET,POST and DELETE methods).

I have deployed the Django code using Heroku, on entering the production URL API endpoints, you can see that the API views can be accessed to anyone (refer below)

https://preview.redd.it/tpjia8dmwgwd1.png?width=1233&format=png&auto=webp&s=7866a8e5957e3704b606a7fab4df01ca19c29064

I want to know how can I hide this page from others accessing it? Or how can I prevent this data being available online? Please help with this.

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

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

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

Читать полностью…
Подписаться на канал