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
Monday Daily Thread: Project ideas!

Comment any project ideas beginner or advanced in this thread for others to give a try! If you complete one make sure to reply to the comment with how you found it and attach some source code! If you're looking for project ideas, you might be interested in checking out Al Sweigart's, "The Big Book of Small Python Projects" which provides a list of projects and the code to make them work.

/r/Python
https://redd.it/uqixn0
Where to put things to be shared among different apps

I'm working on a personal project relating the backend for a point of sale app. I'd like to know the best approach for locating things like Models, Serializers and Views (among other things) that are not related to a specific app and are shared among all of them.

For example, my apps are:

* authentication (relating users)
* client
* quote
* sale

I added a new app called helper which for example has:

* A model module where a have a BaseModel with *created\_at* and *updated\_ad* fields from which most of my models on my apps inherit.
* A views module that has CRDViewSet and CRViewSet, which are Create Read Delete and Create Read ViewSets which inherit from GenericViewSet and the needed mixins, and are used on some views on the other apps.
* A validators module with functions that validate things like a positive value, not negative value, and acceptable future date, among other things which also are validations shared and used in different serializers on my apps.

Is it okay for this *helper* app to exist or is there a more recommended way to have these different structures which are shared among apps?

​

Edit: Thanks for all your answers, I agree that core sounds a lot better

/r/django
https://redd.it/urddpe
Trying to access an element in the dictionary but it's printing the entire dictionary

debug={
"cat":"cat-value",
"dog":"dog-value
}
When I run the following:
{% for cat, value in debug.items %}
{{ value }}
{% endfor %}

The output is "cat-value dog-value", the entire dictionary. How do I only take "cat"

/r/django
https://redd.it/urszoo
Dockerizing a Flask app : FileNotFoundError: Errno 2 No such file or directory: '/app/app.py': '/app/app.py'

Hello everyone, I hope you're doing great.

I'm desperatly trying to dockerize an opensource Flask app (open web calendar).

But I keep getting this error at the end:

FileNotFoundError: Errno 2 No such file or directory: '/app/app.py': '/app/app.py'

But the weird thing is that I get this error from running app.py which is definitely in /app.

I have pushed all the files to a GitHub repo. An other even weirder thing is that if I use git-clone \-> python app.py on my computer, the app works like a charm but if I do the exact same two commands inside a Python 3.7.9 container it crashes with this error (I was using python:3.7.9-slim-buster image)...

This is why I'm quite sure it's not an issue with my Dockerfile.

EDIT: As requested by many, here is the Dockerfile:

FROM python:3.7.9-slim-buster

WORKDIR /app

COPY requirements.txt /app
RUN pip3 install --no-cache-dir -r requirements.txt

COPY . /app

CMD "python3", "/app/app.py"

I am using Python 3.7.9 (as specified in the runtime.txt).

Here is the

/r/flask
https://redd.it/usclze
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/urzy9j
Code cells not showing in HTML

After one of the recent updates I have a problem with HTML-output. The list of possible formats in the “Save as” menu has some new elements. Including “HTML without code”.
However, when I try to save a HTML-file I never get any code cells. Not when using the mentioned alternative (as expected), nor when I use the regular “Save as HTML”- option. The files are identical.


Does anyone have any idea? 💡

/r/JupyterNotebooks
https://redd.it/usemhs
How to Run Jupyter In Any Cloud With TPI Terraform Plugin

The following study shows a few distinct advantages of TPI to solve pain points related to maintaining your software and hardware stack as well as being limited by the hardware you own: [Using Jupyter/TensorBoard In Any Cloud With One Command](https://www.devforce.one/16373548/how-to-run-jupyter-in-any-cloud-with-one-command#/)

* Lower cost: use your preferred cloud provider's existing pricing, including on-demand per-second billing and bulk discounts.
* Auto-recovery: spot/preemptible instances are cheap but unreliable. TPI reliably and automatically respawns such interrupted instances, caching & restoring the working directory in the cloud even when you are offline.
* Custom spec: full control over hardware & software requirements via a single config file.

/r/JupyterNotebooks
https://redd.it/usl77o
Different foreignkey by condition in one model

Hi, I have a website that has a student and a teacher side. As a teacher you can track students progress in an elearning platform. There are 4 different ways that you can track a student.

Quiz, Fillinblanks, Read the chapter, Watched the video. (Each represents a model)

​

I want to make a model that lets the teacher assign homework for the student. I want to be able to make a model that is different depending on which homework you assign (Quiz, Fillinblanks etc..)

​

So if the teacher wants to assign a quiz and a watched the video for chapter 3.1 I want the model to do two rows. One for the quiz and one for the video, linked with foreign keys to the respective model so you track if the homework is done.

​

Is there an easy way to do this without using 8 tables for all of it. I read up on generic relations but I can't really figure out how to apply that to this situation.


Thanks for any help.

/r/django
https://redd.it/ut15qt
Jupyter notebook doesnt show output

so when i use python and try to run a cell more than once once a star (*) appears in the cell number and no output is given. i then have to either delete the cell or create a new file cuz i have no idea how to fix it

/r/JupyterNotebooks
https://redd.it/urrjtp
Understanding Flask authentication, authorisation, session management

### What I do?

A `User` model, most important part of my application usually connected to dozens of other models.

> User enters their unique username and password in a form on /register endpoint and submits.

The information submitted by user is recieved at backend.

I hash the password using `bcrypt` and save it in `password_hash` field.

> For /login I match user password hash with the saved hash.

I use Flask-Login which is a "user session management" library according to documentation.

I add @login_required to routes I need user to be authenticated.

### My burning questions

- What is authentication part of this flow and what is authorisation part?

- What is this "flow" called? Traditional...?

- What is Flask-login doing exactly? What happens if I don't use it?

- I am handling login/signup part almost manually, I'd like to do session part too, how does that work?

- My "API endpoints" are where I recieve json data and return jsonify data and it works fine with the same setup. So why do "APIs need different way to auth/auth"?
Is this because "browser is sending the cookie containing session info"?

- What is the right way to do auth/auth in an application which will be accessed by browsers (both template rendered views and endpoints)

/r/flask
https://redd.it/ut2xbs
I built an appointment scheduler for my school using Django and Bootstrap

/r/django
https://redd.it/uthw9v
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/usqdq0
Using a value from one route to another

Hi everyone, I’m pretty new to flask.
I have a in this route the user variable, but I want to use its value in a another route, so how can I do it?

@app.route('/reset_password', methods=['POST', 'GET'])
def reset_password():
form = request.form
user = Users.query.filter_by(email=form['email-address']).first()
if not user:
flash('User does not exist!!')
return redirect(url_for('forgot_password'))


Edit:
I added:
session[‘user’] = user.id in the reset_password route

And when i try doing:
user_id = session[‘user’] in my other route, it gives me a key error

What can be the problem?

/r/flask
https://redd.it/utvz3w