Conditional Counting
Let's say we have the following simplistic models:
class Category(models.Model):
name = models.CharField(max_length=264)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "categories"
class Status(models.Model):
name = models.CharField(max_length=264)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "status"
class Bet(models.Model):
title = models.CharField(max_length=264)
description = models.CharField(max_length=264)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10)
status = models.ForeignKey(Status, on_delete=models.CASCADE)
Status choices would be: Sold, Not Sold, Reserved, Withdrawn.
My aim is for each category to get the total amount of products and how many were Sold or Reserved.
This would get the total amount of products per category:
qs = Product.objects.all().values("category__name").annotate(total_products=Count("id"))
Including how many were Sold or Reserved:
qs = Product.objects.all().values("category__name").annotate(total_products=Count("id"), total_available=Count(Case(When(status__name__in=["Sold", "Reserved"], then=True), default=False, output_field=BooleanField())))
I am not sure if it's the correct way, total_available appears to return all the products per category.
/r/django
https://redd.it/8hbvca
Let's say we have the following simplistic models:
class Category(models.Model):
name = models.CharField(max_length=264)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "categories"
class Status(models.Model):
name = models.CharField(max_length=264)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "status"
class Bet(models.Model):
title = models.CharField(max_length=264)
description = models.CharField(max_length=264)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10)
status = models.ForeignKey(Status, on_delete=models.CASCADE)
Status choices would be: Sold, Not Sold, Reserved, Withdrawn.
My aim is for each category to get the total amount of products and how many were Sold or Reserved.
This would get the total amount of products per category:
qs = Product.objects.all().values("category__name").annotate(total_products=Count("id"))
Including how many were Sold or Reserved:
qs = Product.objects.all().values("category__name").annotate(total_products=Count("id"), total_available=Count(Case(When(status__name__in=["Sold", "Reserved"], then=True), default=False, output_field=BooleanField())))
I am not sure if it's the correct way, total_available appears to return all the products per category.
/r/django
https://redd.it/8hbvca
reddit
Conditional Counting • r/django
Let's say we have the following simplistic models: class Category(models.Model): name = models.CharField(max_length=264) ...
Nuitka Release 0.5.30
http://nuitka.net/posts/nuitka-release-0530.html
/r/Python
https://redd.it/8ha98n
http://nuitka.net/posts/nuitka-release-0530.html
/r/Python
https://redd.it/8ha98n
Nuitka Home
Nuitka Release 0.5.30
This is to inform you about the new stable release of Nuitka. It is the extremely compatible Python compiler. Please see the page "What is Nuitka?" for an overview.
This release has improvements in al
This release has improvements in al
[AF] How to prevent brute-force login attacks using flask-login?
Is there a standard way of doing this, or is it up to me to implement the logic?
Thanks
/r/flask
https://redd.it/8h8mja
Is there a standard way of doing this, or is it up to me to implement the logic?
Thanks
/r/flask
https://redd.it/8h8mja
reddit
r/flask - [AF] How to prevent brute-force login attacks using flask-login?
4 votes and 6 so far on reddit
how to display conditional html in a template shared between several routes ?
hello, so i have two different routes that return the same template,
@app.route('/')
def home:
...
return render_template('default.html')
@app.route('/name')
def name:
...
name = #some code
return render_template('default.html',name=name)
a chunk of code of that template is the following
{% if name is not none %}
<p>customer name is : {{ name }}</p>
{% endif %}
it appears that in the route ('/'), the name is evaluated as not none, so it goes through the if statement and the html is displayed (with blank where the name is supposed to be displayed)
i want my html part to be either fully displayed with the name if there is one, or nothing. how can i do ?
/r/flask
https://redd.it/8h765l
hello, so i have two different routes that return the same template,
@app.route('/')
def home:
...
return render_template('default.html')
@app.route('/name')
def name:
...
name = #some code
return render_template('default.html',name=name)
a chunk of code of that template is the following
{% if name is not none %}
<p>customer name is : {{ name }}</p>
{% endif %}
it appears that in the route ('/'), the name is evaluated as not none, so it goes through the if statement and the html is displayed (with blank where the name is supposed to be displayed)
i want my html part to be either fully displayed with the name if there is one, or nothing. how can i do ?
/r/flask
https://redd.it/8h765l
reddit
r/flask - how to display conditional html in a template shared between several routes ?
1 votes and 8 so far on reddit
Hello Qt for Python
https://blog.qt.io/blog/2018/05/04/hello-qt-for-python/
/r/Python
https://redd.it/8hdqsh
https://blog.qt.io/blog/2018/05/04/hello-qt-for-python/
/r/Python
https://redd.it/8hdqsh
www.qt.io
Hello Qt for Python
typing: How to use class in type hints while defining the class itself?
Here’s an illustration of what I want to do:
class Foo(object):
def fn(self) -> Foo: # <-- error here
return Foo()
When doing this, the compiler complains:
NameError: name 'Foo' is not defined
How do I use `Foo` in a type hint while defining `Foo` itself?
/r/Python
https://redd.it/8hdnzz
Here’s an illustration of what I want to do:
class Foo(object):
def fn(self) -> Foo: # <-- error here
return Foo()
When doing this, the compiler complains:
NameError: name 'Foo' is not defined
How do I use `Foo` in a type hint while defining `Foo` itself?
/r/Python
https://redd.it/8hdnzz
reddit
r/Python - typing: How to use class in type hints while defining the class itself?
9 votes and 3 so far on reddit
Conditional Counting
Let's say we have the following simplistic models:
class Category(models.Model):
name = models.CharField(max_length=264)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "categories"
class Status(models.Model):
name = models.CharField(max_length=264)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "status"
class Bet(models.Model):
title = models.CharField(max_length=264)
description = models.CharField(max_length=264)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10)
status = models.ForeignKey(Status, on_delete=models.CASCADE)
Status choices would be: Sold, Not Sold, Reserved, Withdrawn.
My aim is for each category to get the total amount of products and how many were Sold or Reserved.
This would get the total amount of products per category:
qs = Product.objects.all().values("category__name").annotate(total_products=Count("id"))
Including how many were Sold or Reserved:
qs = Product.objects.all().values("category__name").annotate(total_products=Count("id"), total_available=Count(Case(When(status__name__in=["Sold", "Reserved"], then=True), default=False, output_field=BooleanField())))
So, if you don't include default=True in the annotation it works as intented.
I want to go a little bit further, so the total_available is a percentage of the total products of each category:
qs = Product.objects.all().values("category__name").annotate(total_products=Count("id"), total_available=Cast((Count(Case(When(status__name__in=["Sold", "Reserved"], then=True), default=False, output_field=BooleanField())) / Count("id")) * 100., FloatField()))
This way it really does not work properly, I wish I knew why.
/r/django
https://redd.it/8hbvca
Let's say we have the following simplistic models:
class Category(models.Model):
name = models.CharField(max_length=264)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "categories"
class Status(models.Model):
name = models.CharField(max_length=264)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "status"
class Bet(models.Model):
title = models.CharField(max_length=264)
description = models.CharField(max_length=264)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10)
status = models.ForeignKey(Status, on_delete=models.CASCADE)
Status choices would be: Sold, Not Sold, Reserved, Withdrawn.
My aim is for each category to get the total amount of products and how many were Sold or Reserved.
This would get the total amount of products per category:
qs = Product.objects.all().values("category__name").annotate(total_products=Count("id"))
Including how many were Sold or Reserved:
qs = Product.objects.all().values("category__name").annotate(total_products=Count("id"), total_available=Count(Case(When(status__name__in=["Sold", "Reserved"], then=True), default=False, output_field=BooleanField())))
So, if you don't include default=True in the annotation it works as intented.
I want to go a little bit further, so the total_available is a percentage of the total products of each category:
qs = Product.objects.all().values("category__name").annotate(total_products=Count("id"), total_available=Cast((Count(Case(When(status__name__in=["Sold", "Reserved"], then=True), default=False, output_field=BooleanField())) / Count("id")) * 100., FloatField()))
This way it really does not work properly, I wish I knew why.
/r/django
https://redd.it/8hbvca
reddit
Conditional Counting • r/django
Let's say we have the following simplistic models: class Category(models.Model): name = models.CharField(max_length=264) ...
Stay DRY in Django With Decorators [xpost r/python]
https://blog.bastions.co/dev/stay-dry-in-django/
/r/django
https://redd.it/8h7bw6
https://blog.bastions.co/dev/stay-dry-in-django/
/r/django
https://redd.it/8h7bw6
reddit
r/django - Stay DRY in Django With Decorators [xpost r/python]
10 votes and 3 so far on reddit
Whats this monitoring package whose name I can't think of?
There's a package that monitors your Django projects performance (in production?) and for a certain subset (i think?) of requests records performance characteristics.
IIRC, it has a page that graphs all the information for you.
Sorry if this is too vague, the fact that I can't remember what this thing is is driving my crazy.
/r/django
https://redd.it/8hh2dl
There's a package that monitors your Django projects performance (in production?) and for a certain subset (i think?) of requests records performance characteristics.
IIRC, it has a page that graphs all the information for you.
Sorry if this is too vague, the fact that I can't remember what this thing is is driving my crazy.
/r/django
https://redd.it/8hh2dl
reddit
r/django - Whats this monitoring package whose name I can't think of?
0 votes and 2 so far on reddit
How to subscript strings in jinja2?
I would like to show only the first 15 characters of "{{ room.text }}", basically the jinja2 version of room.text[:15]
https://imgur.com/a/1jB85Bi
/r/djangolearning
https://redd.it/8he5kt
I would like to show only the first 15 characters of "{{ room.text }}", basically the jinja2 version of room.text[:15]
https://imgur.com/a/1jB85Bi
/r/djangolearning
https://redd.it/8he5kt
[AF] How to improve current application?
Hi, so for I've been working on a website for about a month now and am now starting to get a little tired of my site's overall clunkiness.
One step forward would be to make 2 separate micro-applications. One for the user and one for the post, which will also have comments and topics, to make things a little less complicated to deal with.
How else would I be able to improve my flask site [provided here](https://github.com/ModoUnreal/nuncio)?
Also if you want to contribute then definitely do so, the more the merrier.
/r/flask
https://redd.it/8hi0sd
Hi, so for I've been working on a website for about a month now and am now starting to get a little tired of my site's overall clunkiness.
One step forward would be to make 2 separate micro-applications. One for the user and one for the post, which will also have comments and topics, to make things a little less complicated to deal with.
How else would I be able to improve my flask site [provided here](https://github.com/ModoUnreal/nuncio)?
Also if you want to contribute then definitely do so, the more the merrier.
/r/flask
https://redd.it/8hi0sd
GitHub
ModoUnreal/nuncio
nuncio - The fair news website
search by "name" using form and SQLite 3
Hi guys
I'm playing with flask and i'm trying to display some informations using a form and SQLite3. I . have this code :
Run.py
@app.route('/search', methods=['GET','POST'])
def search():
conn = sql.connect('database.db')
cursor = conn.cursor()
if request.method == 'GET':
cursor.execute('select * from components where name LIKE ?',[request.form['name']])
results = cursor.fetchall()
return render_template("search.html", results = results)
search.html \(index.html is the same code\)
<form action="{{ url_for('search') }}" method=POST class=form-horizontal>
<div class="form-group row">
<label for="inputPassword" class="col-sm-2 col-form-label">Component</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputname" name="name" placeholder="name">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Search</button>
</div>
</div>
</form>
But i still have this error each time
# UnboundLocalError
UnboundLocalError: local variable 'results' referenced before assignment
can someone tell me what i am doing wrong ? \>\_O
/r/flask
https://redd.it/8hf41x
Hi guys
I'm playing with flask and i'm trying to display some informations using a form and SQLite3. I . have this code :
Run.py
@app.route('/search', methods=['GET','POST'])
def search():
conn = sql.connect('database.db')
cursor = conn.cursor()
if request.method == 'GET':
cursor.execute('select * from components where name LIKE ?',[request.form['name']])
results = cursor.fetchall()
return render_template("search.html", results = results)
search.html \(index.html is the same code\)
<form action="{{ url_for('search') }}" method=POST class=form-horizontal>
<div class="form-group row">
<label for="inputPassword" class="col-sm-2 col-form-label">Component</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="inputname" name="name" placeholder="name">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Search</button>
</div>
</div>
</form>
But i still have this error each time
# UnboundLocalError
UnboundLocalError: local variable 'results' referenced before assignment
can someone tell me what i am doing wrong ? \>\_O
/r/flask
https://redd.it/8hf41x
reddit
search by "name" using form and SQLite 3 • r/flask
Hi guys I'm playing with flask and i'm trying to display some informations using a form and SQLite3. I . have this code : Run.py ...
Lot of Django posts I see on Reddit
Hey I just started learning Django and Python. I need some help with how to do this.
‘Posts some complicated shit that makes me think why the hell am I not on this level when I just started learning Django too?’
/r/django
https://redd.it/8hgbol
Hey I just started learning Django and Python. I need some help with how to do this.
‘Posts some complicated shit that makes me think why the hell am I not on this level when I just started learning Django too?’
/r/django
https://redd.it/8hgbol
reddit
Lot of Django posts I see on Reddit • r/django
Hey I just started learning Django and Python. I need some help with how to do this. ‘Posts some complicated shit that makes me think why the...
[R][Neuroscience] Emergence of grid-like representations by training recurrent neural networks to perform spatial localization
https://arxiv.org/abs/1803.07770
/r/MachineLearning
https://redd.it/8hhcx4
https://arxiv.org/abs/1803.07770
/r/MachineLearning
https://redd.it/8hhcx4
reddit
r/MachineLearning - [R][Neuroscience] Emergence of grid-like representations by training recurrent neural networks to perform spatial…
9 votes and 1 so far on reddit
Reddit Bot Introduction - Programming Reddit Bot with PRAW in Python
https://www.youtube.com/watch?v=BaqvfTuHCJk
/r/Python
https://redd.it/8heweg
https://www.youtube.com/watch?v=BaqvfTuHCJk
/r/Python
https://redd.it/8heweg
YouTube
Reddit Bot Introduction - Programming Reddit Bot with PRAW in Python
For this video we will explore building a Bot in Reddit. Links: PycharmEDU - https://www.jetbrains.com/pycharm/ Github Reddit Examples - https://github.com/R...
What workflows are people automating?
I do financial + campaign analytics at my company and lately I’ve been at work building a full campaign forecasting + expensing + ROI calculating pipeline with several other teams. It’s exciting work but it’s mostly just procedural programming with pandas. I’m curious what others are up to. What business processes are you automating, improving or making more efficient?
/r/Python
https://redd.it/8hfx9w
I do financial + campaign analytics at my company and lately I’ve been at work building a full campaign forecasting + expensing + ROI calculating pipeline with several other teams. It’s exciting work but it’s mostly just procedural programming with pandas. I’m curious what others are up to. What business processes are you automating, improving or making more efficient?
/r/Python
https://redd.it/8hfx9w
reddit
What workflows are people automating? • r/Python
I do financial + campaign analytics at my company and lately I’ve been at work building a full campaign forecasting + expensing + ROI calculating...
The time before a page is loaded in the browser is very slow on list views with around 400 objects, what can I do to improve that and there what tools do you suggest to profile where the bottlenecks lie?
I can paginate generic list views, and that does improve the time for the page to be served.
But I have some other list views which use FilterSet from django-filter, and those can't be paginated(well, out of the box, at least)
They can take around 16 secs to fully send the page to the browser, and that's just for around 400 objects. I know the problem is not database related, because according to django-debug-toolbar the SQL quiries finish around 150 ms.
Are these serving times normal?
/r/djangolearning
https://redd.it/8hjgp4
I can paginate generic list views, and that does improve the time for the page to be served.
But I have some other list views which use FilterSet from django-filter, and those can't be paginated(well, out of the box, at least)
They can take around 16 secs to fully send the page to the browser, and that's just for around 400 objects. I know the problem is not database related, because according to django-debug-toolbar the SQL quiries finish around 150 ms.
Are these serving times normal?
/r/djangolearning
https://redd.it/8hjgp4
reddit
The time before a page is loaded in the browser... • r/djangolearning
I can paginate generic list views, and that does improve the time for the page to be served. But I have some other list views which use FilterSet...
[AF] Getting error 404 when using search bar?
Hi, so I want to make a search-bar which searches the database for posts with the same name as the query.
Here is the current code I have in my routes.py file:
@app.route('/search_result/<search_str>', methods=['GET'])
def search_result(search_str):
post_query = Post.query.filter_by(title=search_str).all()
topic_query = Topic.query.filter_by(tag_name=search_str).all()
return render_template('search_result.html', post_query=post_query, topic_query=topic_query)
@app.route('/search', methods=['GET', 'POST'])
def search():
form = SearchForm()
if request.method == 'POST' and form.validate_on_submit():
search_str = form.search_str.data
return redirect(url_for('search_result', search_str=str(search_str)))
return redirect(url_for('search_result', search_str=form.search_str.data))
And here is the code for the base.html file:
<div class="searchbar">
<form action="{{ url_for('search') }}" method=post>
<input type="text" name="search" placeholder="Search...">
</form>
</div>
So my problem is this. Whenever I make a search using the input, for some reason it is never saved and an error 404 is made. What should happen is that the search() function would take the inputs from the form and redirect to the search_result route.
What exactly is going on?
Here is my github repo for it if you need more context:
https://www.github.com/ModoUnreal/dopenet
/r/flask
https://redd.it/8gruit
Hi, so I want to make a search-bar which searches the database for posts with the same name as the query.
Here is the current code I have in my routes.py file:
@app.route('/search_result/<search_str>', methods=['GET'])
def search_result(search_str):
post_query = Post.query.filter_by(title=search_str).all()
topic_query = Topic.query.filter_by(tag_name=search_str).all()
return render_template('search_result.html', post_query=post_query, topic_query=topic_query)
@app.route('/search', methods=['GET', 'POST'])
def search():
form = SearchForm()
if request.method == 'POST' and form.validate_on_submit():
search_str = form.search_str.data
return redirect(url_for('search_result', search_str=str(search_str)))
return redirect(url_for('search_result', search_str=form.search_str.data))
And here is the code for the base.html file:
<div class="searchbar">
<form action="{{ url_for('search') }}" method=post>
<input type="text" name="search" placeholder="Search...">
</form>
</div>
So my problem is this. Whenever I make a search using the input, for some reason it is never saved and an error 404 is made. What should happen is that the search() function would take the inputs from the form and redirect to the search_result route.
What exactly is going on?
Here is my github repo for it if you need more context:
https://www.github.com/ModoUnreal/dopenet
/r/flask
https://redd.it/8gruit
GitHub
ModoUnreal/dopenet
dopenet - The ideal template to make websites in 2018!!!! (I'm kidding of course)