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
Where is self.client inherited from?

The [official documentation](https://docs.djangoproject.com/en/4.1/topics/testing/tools/#default-test-client) states:

> Every test case in a django.test.*TestCase instance has access to an instance of a Django test client. This client can be accessed as self.client.

And sure enough, this works. But:

from django.test import TestCase

dir(TestCase)

returns no method called "client".

Where is self.client inherited from?

/r/djangolearning
https://redd.it/xoeqv0
validate value calculated from another field

i need to validate a field in django admin.

the field is calculated based upon a value of another field.

i've tried several solutions but none could work like i intended, for example:

1. i calculated the value before saving and then raise a validation error but i didn't manage to make the error pop inside the form (as a red string like the validator already build inside django)
2. i tried to use a validator already build in but i didn't know how to reference the other field inside the validator. ** this would be the ideal solution since its the cleanest**
3. i tried to manipulate the admin form to validate the value but the value is not yet calculated in there as far as i know. in this attempt i also tried to make the field of interest ( the one that get calculated) as a property using a decorator so it would be calculated before validation

what i am trying to achieve is to create a Trade model where i insert the amount of capital to invest and the risk per share i want to take. the idea come from this blog. basically i'm trying to replicate the functionality of the Money

/r/djangolearning
https://redd.it/xohm8s
How I deploy my bootstrapped Python webapp with 150k monthly users

I am a one-man show building a web-based software product. Some quick facts about my app:

Written in Python Flask
150k visitors per month
15k registered users
3k US$ revenue per month
70 requests per second at peak-time

This is a technical post looking at the infrastructure that runs my app with a focus on how I deploy it.

## Summary

I use 2 VPS (virtual private servers) running on DigitalOcean
The database is Postgres and is fully managed by DigitalOcean
I use a blue-green deployment
Deployment is done via `git` and `ssh`.
No CI/CD
No containers
Absolutely no Kubernetes

I am a strong advocate of using "boring technology". I am also the only developer of the app, which makes many things simpler.

## The application infrastructure

The app is a CRUD monolith that is deployed as one artefact. I use nginx and gunicorn to serve the app.

The app runs on 2 virtual private servers, one of which is the production server, the other of which is the staging server. During deployment, they switch roles. This is a so-called blue-green deployment, more on that later.

I'm using a DigitalOcean droplet with 8 shared CPUs, 16GB of memory and 80 GB of storage for each server.

/r/Python
https://redd.it/xobba8
Does some CSS work differently in Django?

I have this CSS. It works fine when I use it for a regular site from just plain HTML and CSS. When I use this with my Django template it acts differently. What I expect to happen is the navigation bar will be flush with the top of the screen. This happens with the plain HTML site. In Django, however there is a gap at the top where you can see the body of the page.

`* {`

`-webkit-box-sizing: border-box;`

`-moz-box-sizing: border-box;`

`box-sizing: border-box;`

`}`

​

`body {`

`margin-top: 90px;`

`background-color: lightgray;`

`}`

​

`.navi {`

`position: fixed;`

`height: 45px;`

`top: 0;`

`right: 0;`

`left: 0;`

`background-color: black;`

`color: white;`

`padding: 5px`

`}`

​

`.navi-link {`

`color: white;`

`text-decoration: none;`

`padding: 5px;`

`}`

Edit: Here is the HTML

In Django:

`{% load static %}`


`<!DOCTYPE html>`
`<html lang="en">`
`<head>`
`<meta charset="UTF-8">`
`<link rel="stylesheet" href="{% static 'css/style.css' %}">`
`</head>`
`<body>`
`<header>`
`{% include 'navi.html' %}`
`</header>`
`{% block content %}`


`{% endblock %}`


`</body>`
`</html>`

Regular HTML:

`<!DOCTYPE html>`

`<html lange='en'>`

`<head>`

`<meta charset='UTF-8'>`

`<title>Practice CSS</title>`

`<link rel='stylesheet' href='style.css'>`

`</head>`

`<body>`

`<header class='navi'>`



`<a class='navi-link' href='index.html'>Home</a>`

&#x200B;

`</header>`



`<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo

/r/djangolearning
https://redd.it/xox2gy
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/xp0c2v
How to modify POST data before it gets validated without throwing a ValueError exception?

I am trying to convert a date that is created by a frontend datetime library to a iso formatted date.

I do this using python's strptime function, which throws an exception if it can't format the date. I do this in my view and Django shows an exception page in that case, but I want Django to throw a normal ValidationError that can be seen in the frontend.

I tried writing a clean_date() function in my form to modify the date but Django's DateTimeField validator runs before clean_date() and so my code is never run.

/r/django
https://redd.it/xpn1bw
Videos in HTML

Hello,

I am working on a project, and I am trying to get a video playing in the DOM but I am having lots of trouble.

I have done a lot of researching but I cannot find the actual problem. The videos I am trying to play are MP4 but I can convert them if needed.

Everything works fine in non-Chromium based browsers like Safari or FireFox. But in Chromium browsers, the video loads fine but anytime seeking is requested, the video restarts.

I have tried with the video controls in the browser as well as programmatically with JavaScript, both just jump to the beginning of the video.

Also, I do not know if this is supposed to happen, but in all browsers it makes three total requests.

(Copied from Google Chrome Dev Tools as fetch):


1st:

GET /media/inspection_media/test.mp4 HTTP/1.1
Accept: */*
Accept-Encoding: identity;q=1, *;q=0
Accept-Language: en-US,en;q=0.9
Connection: keep-alive
Cookie: csrftoken=token; sessionid=id
Host: localhost:8000
Range: bytes=0-
Referer: http://localhost:8000/home/%5Eredirected/(%3FP1%5Cd+)/$
Sec-Fetch-Dest: video
Sec-Fetch-Mode: no-cors
Sec-Fetch-Site: same-origin
User-Agent:

/r/django
https://redd.it/xppsmw
Running Python Script Online

Hey Guys

I made a script and its running perfectly. The script logs in to a webpage and checks something after that it sends me an email. I used selenium for it.

Now is my question if there is a service where I can run this script like once a hour. Whats the best service to do this?

My first guess was to just use the win10 taskscheduler but it was not reliable.

/r/Python
https://redd.it/xpoxrh
How to use remote jupyter kernel with vscode in my local pc?

Hi, I want to use a python environment in remote server together vscode jupyter notebook extension in my local pc. I used remote extension in vscode and added remote jupyter server but I don't want to use the file in remote machine. I wanna use the files in my local pc with remote jupyter kernel. So, I just wanna use remote computing with jupyter kernel and store my data in my local pc. Is it possible?

https://preview.redd.it/llcfupk8d0o91.png?width=631&format=png&auto=webp&s=e15858ed524d133daf17c24c686af83ec631c4e0

/r/JupyterNotebooks
https://redd.it/xeu1pl
How to use SageMath use Jupyter

I was wondering how to use sagemath with jupyter. I am not experienced with jupyter or sagemath though I was able to install jupyter and get it running. However I am not sure if I have installed sagemath correctly and wasn’t sure how to setup it up so my notebooks can use sagemath. Any help or resources are greatly appreciated thanks!

/r/JupyterNotebooks
https://redd.it/xejik2
D Any Python Code available for Visualizing Named-Entities and Relations?

I'm looking to visualize named-entities and relations over a very large set of documents and saving this in some external document (like a PDF or HTML file). Specifically, given that I have the document locations corresponding to the entities and relations, I am looking for something to label every entity with a specific color corresponding to its entity type and colored arrows corresponding to relation type between related entities. Is there any python code available to do this?

I tried generating a PDF using PyFPDF but it won't easily let me color individual words. For now, I am giving up on PyFPDF and trying to use python to generate HTML and CSS.


UPDATE: Turns out I can use PyFPDF to color individual words (using the write function rather than the cell/multicell functions). However, still trying to figure out how to draw arrows between words. Any suggestions for libraries that can do this easily would be appreciated!

/r/MachineLearning
https://redd.it/xq61zi
Is there a way to use Store Magic across 2 Jupyter notebooks (one running Python & one running R)?

I've used the store magic for 2 python notebooks and that worked well. Just curious if there is a way to share data between a R notebook and a Python notebook.

Thanks in advance.

/r/JupyterNotebooks
https://redd.it/x578xu
I built a platform for helping Django developer to find a new job.. and it's work pretty well !

Hey r/Django :),

I'm a freelance Django dev and I'm making some side projects (Big fan of the indie hacking movement)

My last one is a platform for helping Django developers to find a new job or a new freelance contract (https://www.djangotop.com).It's 100% free. I put it online 1 month ago and I already have 70 devs that are signed up :D (and 2 found a job !)

Do not hesitate to give feedback and if you are interested, create your profile ! It helps me to be able to bring back more traffic from companies interested in Django developers !

/r/django
https://redd.it/xq7jlx
D DALL·E Now Available Without Waitlist

https://openai.com/blog/dall-e-now-available-without-waitlist/

It appears to work as advertised, not any special workflow. (as a bonus, it does work with organizations too, with credits shared)

/r/MachineLearning
https://redd.it/xqhho8
Made a private social network with flask.

Here is the landing page: vomindo.pythonanywhere.com

If you want to test it out you can dm me and I will send you an invite link.

Any feedback on the landing page is welcomed. I kinda need some help to figure out how to make it better.

/r/flask
https://redd.it/xq5nyq
Related object is literally being saved immediately above but still getting "ValueError("save() prohibited ... unsaved related object 'url'.")

Django==4.0.7 (I can't use 4.1 yet because of django-celery-beat not working with 4.1)


Getting this error for a updateorcreate() using a related object (code line 392). Django thinks it's unsaved but it's literally being saved right above it. This only happens when the get() fails and I'm creating the object after URL.DoesNotExist is caught. If the get() works the updateorcreate() is handled without issue.

How can I save it more than it's already being saved? :-)

(Traceback is at the bottom of this post)

Code:


378 # Handle URL key
379

/r/djangolearning
https://redd.it/xqs3m4
Thursday Daily Thread: Python Careers, Courses, and Furthering Education!

Discussion of using Python in a professional environment, getting jobs in Python as well as ask questions about courses to further your python education!

This thread is not for recruitment, please see r/PythonJobs or the thread in the sidebar for that.

/r/Python
https://redd.it/xqsk02
How to reformat IPYNB files for JSON compatability?

When dragging and dropping the .IPYNB file in Jupyter Notebooks, I get an error...

The error was: SyntaxError: Unexpected end of JSON input

when inspecting the properties I see:

\-------------

Type of file: IPYNB

Location: 04/demos/demo/titanic/notebooks/

size: 82 KB

compressed: 25 KB

Method: Deflated

CRC-32: 5570B0C4

Index: 420

\--------------

How can I reformat these files with the correct JSON input so I can use IPYNB files in Jupyter Notebook?

/r/JupyterNotebooks
https://redd.it/xqm13a