#web-development

2 messages · Page 2 of 1

brittle copper
#

Don't tell me I didn't write that and can't xD

#

If somehow you are interested that takes the strings in two backticks() and returns it as <a href="two_backticks">two backticks</a>`

#

Oh god

deep cave
#
@register.filter(name="replacer")
def messagetolink(text):
    split = text.split('`')
    even, odd = split[0::2], split[1::2]
    return format_html_join('', '{}<a href="' + site_name + '{}">{}</a>', ((a, ("_".join(b.split())), b) for a, b in itertools.zip_longest(even, odd, fillvalue='')))

# let's first clean up the function name.
def message_to_link(text):

# now let's rip that generator out of the return statement and put it on a line of its own
gen = ((a, ("_".join(b.split())), b) for a, b in itertools.zip_longest(even, odd, fillvalue=''))

# that's a bit better already, let's see how that looks
@register.filter(name="replacer")
def message_to_link(text):
    split = text.split('`')
    even, odd = split[0::2], split[1::2]
    gen = ((a, ("_".join(b.split())), b) for a, b in itertools.zip_longest(even, odd, fillvalue=''))
    return format_html_join('', '{}<a href="' + site_name + '{}">{}</a>', gen)
#

it's still hideous though.

brittle copper
#

I'm so irritated by this that I'm not even trying to add any more this kind of functionality, if I were able to do this I was planning on letting users share links to other websites too

#

But it is so damn incredibly hard for some reason

deep cave
#

I wish we could use an f string for the second argument in format_html_join, but because django is hijacking the {}, we can't do that.

brittle copper
#

I wish it was possible to just mark_safe_permanantly(string)

deep cave
#
# something like this
return format_html_join('', f'{}<a href="{site_name}{}">{}</a>', gen)

# would result in
SyntaxError: f-string: empty expression not allowed
#

so you'd need to escape them

#

and that's just even uglier

brittle copper
#

Anyway, for now I'm happy that I can let users toggle their posts' visibility

deep cave
#

If somehow you are interested that takes the strings in two backticks() and returns it as<a href="two_backticks">two backticks</a>

#

wait, give me an example of that

#

input and output data

#

and on discord backticks are special characters so if you're gonna type them, escape them with backslash

brittle copper
#

I couldn't escape them, I tried, struggled just couple mins ago so I'm just gonna use | instead of backtick :D

deep cave
#

`````

brittle copper
#

this is an example where |this part of the string| and |this part of the string| is converted to local links.

#

this is an example where <a href="http://127.0.0.1:8000/this_part_of_the_string">this part of the string</a> and <a href="http://127.0.0.1:8000/this_part_of_the_string">this part of the string</a> is converted to local links.<a href="http://127.0.0.1:8000/"></a>

#

While you are at it you can add a little feature like if a string is in between two *, the functions returns them as outside links :D

#

But that is purely optional :D

deep cave
#

while I'm at it? I never said I was gonna do this :D

brittle copper
#

Oh, alright, I thought you wanted to try to rewrite that hideous function :D

signal karma
#

Can someone assure me that using flask.g is request specific? If I use it to store user information on a request (to be able to identify who is making the request, before it happens(in a decorator)), is it ever going to collide if there are two request by two different users at the same time?

#

I'm not 100% sure from the doc

#

And it's application critical if it collides

deep cave
#

it used to be. but isnt in the latest version. they changed it in like 0.10 or something

#

but whether things will collide or not is still an interesting question

#

g is in the application context in the latest version - but should still theoretically be wiped for every new request.

#

but the advantage of having it belong to the application context is that you can access it before the request is made. like in the before_request function if you're doing that. which used to be impossible.

#

so to answer your question - it should theoretically be safe from collision, and wiped for every request cycle. though I have never done any kind of extensive testing to that end.

#

I probably would do some tests before relying on it if it was "application critical"

signal karma
#

okok thanks

brave mantle
#

I think it's a threadlocal

#

So it shouldn't conflict as long as everything is on the same thread

#

Python 3.7 will add tasklocals to asyncio which is gonna be awesome

signal karma
#

On the following code:

@staticmethod
@namespace.marshal_list_with(article_full, envelope='articles')
def get():
    return Article.query.all()

I want to return a filtered(reduced) relationship of Article, should I go for something like: Article.query.join(ArticlePrice).filter(ArticlePrice.location_id=req_location).all()?

brittle copper
signal karma
#

I don't know what it is expecting as parameter since it is not detailed 🙂

#
@namespace.marshal_list_with(local_article_full, envelope='articles')
def get(self, location):
    return Article.query\
        .filter(Article.prices.any(active=True), Article.prices.any(location_id=location))\
        .add_columns('price', Article.prices.price)\
        .all()
#

I'd like to get ALL Articles from the provided location, that are active=True and add a column to add its local price(add column 'price' equals to Article.prices.price where Article.prices.location_id equals to parameter location).

#

If that makes sense🤐

#

Note: The provided code is not working obviously

signal karma
#

Basically, I want that in Flask-SQLAlchemy:

SELECT a.*, ap.price
FROM article a
INNER JOIN article_prices ap ON a.id = ap.article_id
WHERE ap.location_id = %location%
AND ap.active = true
signal karma
#

@deep cave Do you have any tips on that? (sorry for pinging 😕 )

brave mantle
#

@signal karma What you want is a relationship

#

Check the sqlalchemy docs on relationships, there are specific ways to define them

signal karma
#

I do have a relationship

#

But then it returns me an array.

#

not a specific value

brave mantle
#

What's wrong with that?

signal karma
#

I want only the value ap.price WHERE ap.location_id = %location%

brave mantle
#

Realistically that's down to how your models are defined

signal karma
#

seems like not since SQL can get it, and I can't figure out using SQLAlchemy

#

syntax problem

brave mantle
#

It sounds like it's possible that there could be more than one result for that, as far as SQLA understands

#

Or it's just that your relationship is defined incorrectly

#

The relationships page gives you a set of different types of relationship

#

It sounds like you need one-to-one or many-to-one but you're using one-to-many or many-to-many

#

You should also make sure your model enforces whatever relationship that you pick

#

Application logic isn't enough

signal karma
#

@brave mantle sorry delay, I was on the SO 2018 survey 😛

#

I think you're missing the point. The SQL request I sent above is giving me the correct result, why should I change my models if SQL can get me the right result? I just want to port this SQL request to a Flask-SQLAlchemy syntax.

brave mantle
#

What's the point of having an ORM if you don't use it?

#

Use the ORM, it saves a lot of time

signal karma
#

exactly

#

that's why I wanna use the sqlalchemy syntax

brave mantle
#

No, you don't get it

#

That isn't using the ORM

#

The method is undocumented because it's used internally, you're not supposed to use it yourself

signal karma
#

I know what you mean. But my model should be fine, I just need to know what syntax I should use to get it right.

#

hm

#

but then maybe I should not use this one add_columns, but there should be a way to translate this sql into flask-sa

brave mantle
#

Define the relationship properly and use that

signal karma
#

it is

#

I get an array of prices

#

I want to get only one out

#

how am I supposed to change my model for that?

brave mantle
#

Then it's not defined correctly

#

I'm cooking atm, I'll get you an example when I can

signal karma
#

but, an Article has multiple Prices. Just when a user request the list of Artciles for a specific location, I want to return just the price of this location, not the array of prices.

#

alright 😉 🍳

brave mantle
#

Well, I think the array is still an SQLA object, you should be able to filter on it

#

An alternative would be to take the approach you need for many-to-many and make a separate relationships table you can join

signal karma
#

that's what I have

#

Hm, more like an extra data table maybe

brave mantle
#

Wait, you said that locations have a price already

#

Can't you just get the price from the location then?

signal karma
#

that's what I want!

#

here are the tables

brave mantle
#

So.. Define relationship in location and..

#

Use it?

signal karma
#
class Article(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(255), nullable=False)
    description = db.Column(db.Text)
    image = db.Column(db.String(255))
    brand = db.Column(db.String(255), nullable=False)
    category_id = db.Column(db.Integer, db.ForeignKey("category.id"), nullable=False)
    prices = relationship("ArticlePrice", cascade="all, delete-orphan")
class ArticlePrice(db.Model):
    article_id = db.Column(db.Integer, db.ForeignKey("article.id"), primary_key=True)
    location_id = db.Column(db.Integer, db.ForeignKey("location.id"), primary_key=True)
    price = db.Column(db.Float, nullable=False)
    active = db.Column(db.Boolean, nullable=False)
    location = relationship('Location')
#

If that help =x

#

And this is not working

#
def get(self, location_id):
    return Article.query\
        .filter(Article.prices.any(active=True), Article.prices.any(location_id=location_id))\
        .all()
brave mantle
#

Eh? I've never seen .any() used

signal karma
#

ah

#

Stackoverflow

#

but I am sure I can't make it work because I don't know the verbs

brave mantle
#

If you can gimme like half an hour or three quarters of an hour I should be able to show you what I mean

#

It you can't wait, head to github, GlowstoneMC/Site

#

We used it there

signal karma
#

I am trying this for days, I think I can wait 😃

brave mantle
#

Haha, okay

brave mantle
#

@signal karma right, okay

#

Say we have a news post

#

News posts belong to single users

#

user = relationship("User", back_populates="news_posts")

#

They own multiple news posts
news_posts = relationship("NewsPost", back_populates="user", cascade="all, delete, delete-orphan")

#

The important thing here is back_populates="user"

#

Each post will have a single user association

#

but to complete that, you need to store the ID in the post, obviously

signal karma
#

ok, fine so far

brave mantle
#

user_id = Column(Integer, ForeignKey("user.id"))

#

Alright, so if we want to get our latest three news posts

#

we might do

#
news_posts = db_session.query(NewsPost).filter_by(published=True).order_by(NewsPost.posted.desc())[0:3]
signal karma
#

now what if you want to return the whole user information with a key last_post?

brave mantle
#

and our User object will be available at post.user

#

OK, so you take your NewsPost query

#

filter by the user

#

so eg filter_by(user=User(whatever))

#

and order_by descending

#

and .one() it

#

then you'll get their latest news post, with the user object attached

#

If all you have is the user ID, you can filter_by(user_id=whatever) as well

#

an example of using .one():

try:
    news_post = db_session.query(NewsPost).filter_by(id=post_id, published=True).one()
except NoResultFound:
    raise HTTPNotFound()
signal karma
#

I will try something

#

@brave mantle How do you get the user information while requesting NewsPosts?

#

no join?

brave mantle
#

@signal karma It's automatic

#

That's the point of specifying the relationship

signal karma
#

ah because of the backref?

brave mantle
#

It does the join for you

signal karma
#

is it the backref?

brave mantle
#

The backref helps, yeah

signal karma
#

ha, new one, how would you get the User with last_posted_date?

brave mantle
#

Find the relevant news post

#

grab the .user from it

#

You'd have to do that in my model

#

But if you actually wanted a last post date for users, you should have that on the user object and update it when they post

signal karma
#

hm =/

#

I can't do that in my case

brave mantle
#

Why not?

signal karma
#

because it has multiple prices at the same time depending on the location, there is no one more relevant than another.

#

to be stored in articles

#

The thing is that in your example, you're gonna get the NewsPost and the user in news_post.user, right? What if you need all informations mixed at the root?

brave mantle
#

what? "all informations mixed at root"?

signal karma
#

It's a bit stupid in your example, but it'd look like:

news_posts = db_session.query(NewsPost).filter_by(published=True).order_by(NewsPost.posted.desc())[0:3]
print(news_posts[0].username)
brave mantle
#

The answer is, well, you don't

signal karma
#

instead of print(news_posts[0].user.username)

brave mantle
#

that'd be news_posts[0].user.username

signal karma
#

right

brave mantle
#

You don't want it mixed

#

that's pretty bad sql design

signal karma
#

okok

brave mantle
#

Ok, look at it this way

#

you have a set of tables

#

each table should represent one particular thing

#

and everything that belongs to that thing

#

When you're writing SQL yourself and doing joins yourself, you get mixed data from each table you joined, but that's messy and doesn't actually represent the structure of your database

#

Under the hood, SQLA still does those joins for you

#

But they're abstracted away, so you have something that actually looks and behaves like regular Python objects

#

that's the point of an ORM

#

Keeping your data compartmentalised like that is important for maintaining a piece of software that is clean and easy to work with

signal karma
#

Sure, but I don't think what I am asking is irrelevant, just it doesn't match 100% your use case

brave mantle
#

You're conflating direct DB access with using an ORM

#

If you want direct DB access, SQLA provides a lower-level API for that purpose

#

but chances are you don't, and the ORM is designed so that you don't have to write or generate a single line of SQL yourself

#

When you have well-defined models like I showed you, you basically have a schema of the attributes to expect on that object

#

That's why your use-case doesn't actually make sense

#

You'd be adding attributes that aren't actually present in your definition

signal karma
#

So how would you design an article table that has multiple prices depending on a location?

brave mantle
#

You would probably need a relationship table

signal karma
#

I do

brave mantle
#

article ID and location ID mapping to price IDs

signal karma
#

hm I don't have a price id...

brave mantle
#

every table should have an ID column

#

your SQL server uses it for indexing

#

usually, you want that to be some numerical ID primary key

signal karma
brave mantle
#

Essentially what you have is a table with a primary key (ID) and a bunch of foreign keys

#

then you provide it in your relationship using the secondary param

#

branches = relationship("ProductBranch", secondary="product_branch_association")

#

if you don't need the backref, don't make it

#

otherwise, if you have extra data in your association, you map it directly

#

It's just like any other relationship, just with a table in the middle

#

so you'd end up with something like article.article_price_assoc.price

#

you could put a property on the model if you wanted it to be just price but you wouldn't be able to set it with .price unless you define a setter

signal karma
#

FYI, if you are interested 😛

brave mantle
#

Intredasting

compact pelican
#

for anyone familiar with knockout, how would i go about moving the results from one table to another when clicked on? i currently have it so when you click on a result it redirects you and instead generates a report based on params associated with the selected result however i would like to move it instead to a new "queue" table where when i click a button all results in the "queue" table get generated at the same time

signal karma
#

@compact pelican what you're asking is very unclear to me..

compact pelican
#

@signal karma ive since fixed the issue i needed to point my nested databind to the parent forgot to do that

compact pelican
#

is anyone in this channel any good with knockout.js? if so could you please DM me? been having some issues with a multi-select report generation page and cannot figure out the multi-select. I have the generation working and it works with just one but i cannot for the life of me figure out how to get multiple pages to generate. Id explain more in this channel but i would just be blowing it up

brittle copper
#

Oh god I can't seem to find a solution to this

Title.objects.order_by("-entry__entry_date2").distinct()```
.distinct() simply doesn't work, apparently it is somehow related to me using order_by and distinct in the same queryset concurrently
#

halp me

brave mantle
#

need more context

brittle copper
#

django

#

Entry class has a foreign_key with Title, and I'm trying to sort Title objects with their entry dates

brave mantle
#

but why distinct?

brittle copper
#

When an entry is entered to a Title,

#

Otherwise if users enter entries to the same Title over and over again my index would show only the same Title after a while

brave mantle
#

surely your application should not be inserting duplicates?

brittle copper
#

So if even though the last 2 entry is entered to the Title x, I only want one title x in my query set

#

Uhm, what? :3

#

Pardon my weak English :8

brave mantle
#

Distinct is for filtering out duplicate values

#

Why are you inserting duplicates?

brittle copper
#

I am not, the ordey_by does it

brave mantle
#

If you weren't inserting duplicates, there would be no duplicates

brittle copper
limber adder
#

i dont know if this is relevant, but in sql distinct comes before order by

brittle copper
#

Tried that too, still the same

#

Help me or I'll suicide by drinking 50 more cups of coffee

frigid hamlet
#

untrasted domain@brittle copper

#

for picture

brittle copper
#

Gimme a sec

#

@frigid hamlet

deep cave
#

distinct takes an optional arg of what col to filter distinct by

#

make sure it is using the col you expect it to by manually specifying it.

#

oh shit was scrolled up

brittle copper
#
from mainsite.customcustom import EMAIL_USE_TLS, EMAIL_HOST, EMAIL_HOST_USER, EMAIL_HOST_PASSWORD, EMAIL_PORT

EMAIL_USE_TLS = EMAIL_USE_TLS
EMAIL_HOST = EMAIL_HOST
EMAIL_HOST_USER = EMAIL_HOST_USER
EMAIL_HOST_PASSWORD = EMAIL_HOST_PASSWORD
EMAIL_PORT = EMAIL_PORT

#

As soon as I add the above code to my settings.py I get this beautiful error

django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.```
#

halp

#

Okay, fixed it, I'm a moron

stone pelican
#

Same

deep cave
#

the fuck. why would you possibly do x = x

brittle copper
#

It was from a tutorial, thought it was a django thing

void summit
#

lol

#

this=this

#

self=self

deep cave
#

what a tutorial.

whole smelt
#

That is next level programming

brittle copper
#
def changestyle(request):
    if request.user.is_authenticated():
        userstyleconfig = UserStyle.objects.get(user_name=User.objects.get(username=request.user.get_username()))
        referer = request.META.get('HTTP_REFERER')
        if userstyleconfig.user_style == "standard":
            userstyleconfig.user_style = "darkorange"
            userstyleconfig.save()
        else:
            userstyleconfig.user_style = "standard"
            userstyleconfig.save()
        if referer == f"{URL}change/changestyle/" or None:
            return HttpResponseRedirect(URL)
        return HttpResponseRedirect(referer)
    else:
        return HttpResponseRedirect(URL)```
#

ERR_TOO_MANY_REDIRECTS

#

halp

#

Okay, I'm a moron, nevermind

pastel hinge
#

hello guys plse can someone refer me to a book on python web development

#

intermediate level

frosty star
#

I am working with django atm, i have a problem with moving the templates folder from app to project folder can someone help me?

brittle copper
#

Hmm, getting closer to the end I have a question if I did something uhm, incorrectly

#

My like, dislike and favorite functions are simply called with the posts id like www.wb.com/like/15/

brave mantle
#

GET requests?

brittle copper
#

Uhm, what exactly do you mean

#

I was gonna ask if I should make the functions in a form and the view called from just www.wb.com/like/ and with a post request

#

No idea how to do it with a get

brave mantle
#

Yeah, that's what I'd recommend doing

#

If you're using JS it doesn't actually have to be a form, but

#

You can just do an xmlhttprequest, which is a bit of a mouthful

#

we call them XHRs for short

brittle copper
#

How am I gonna know the post id if I don't do a form :3

brave mantle
#

Depends how your posts are defined

#

Here's an example XHR from one of my other projects

brittle copper
#
<span class="rate-options"><a data-href="http://127.0.0.1:8000/like/454/" class="like" title="şükela!">
                                    <svg class="dic" id="svg-chevron-up">
                                        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#dic-chevron-up-thick"></use>
                                    </svg></a><a data-href="http://127.0.0.1:8000/dislike/454/" class="dislike" title="çok kötü"><svg class="dic" id="svg-chevron-down">
                                        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#dic-chevron-down-thick"></use>
                                    </svg></a>
                                </span>```
brave mantle
#
    let oReq = new XMLHttpRequest();
    oReq.addEventListener("load", req_listener);
    oReq.open("POST", "/add_object");

    oReq.setRequestHeader("Content-type", "application/json");
    oReq.send(JSON.stringify({"type": type, data: obj, layer: layer}));
brittle copper
#

hmm

brave mantle
#

well, yeah, you answered your own question

#

use an attribute on the html

brittle copper
#

data-id and then post it with js

brave mantle
#

data-post-id="whatever" yeah

brittle copper
#

But I though POST request had to go into a form :3

#

Don't they

brave mantle
#

Nah

#

They're just verbs

#

forms are usually POSTed, but they don't have to be

brittle copper
#

Oh I see

#

One last question

#

Normally I do <form method="POST"> and in django request.POST.get(name)

#

Oh fuck

#

Okay I'm an idiot, nvm :D

#

Thank you :D

brave mantle
#

:>

brittle copper
#

Damn :(

brave mantle
#

?

brittle copper
brave mantle
#

Check the console then

#

lol

#

Are you building imgur?

brittle copper
#

what

brave mantle
#

xD

deep cave
#

I built an imgur for work once.

#

that was a fun project

brave mantle
#

I bet it was

#

I hope you didn't store the images using git LFS

#

:P

brittle copper
#

halp me gays

#

guys*

deep cave
#

check your logs

brave mantle
#

breh

#

yeah, check your logs

brittle copper
#

good idea

deep cave
#

internal server error always means "check your logs"

brave mantle
#

Yup

deep cave
#

in fact, most of the time you should just have a tail -f running to the relevant logs while doing webdev

brave mantle
#

a 500 is the webserver going "something broke blobderpy"

deep cave
#

I basically have a monitor dedicated to that :P

brittle copper
#

Uhm, should I return something if I only do some logic

brave mantle
#

"I just don't know what went wrong"

deep cave
#

you don't have to return anything.

brittle copper
#
def like(request):
    userchoice = UserStyle.objects.get(user_name=User.objects.get(username=request.user.get_username()))
    entry_pk = request.POST.get("getnew", 246)
    entry = Entry.objects.get(pk=entry_pk)
    if entry in userchoice.user_dislike.all():
        userchoice.user_dislike.remove(Entry.objects.get(pk=entry_pk))
        userchoice.user_like.add(Entry.objects.get(pk=entry_pk))
    elif entry in userchoice.user_like.all():
        userchoice.user_like.remove(Entry.objects.get(pk=entry_pk))
    elif entry not in userchoice.user_like.all() or userchoice.user_dislike.all():
        userchoice.user_like.add(Entry.objects.get(pk=entry_pk))
    entry.entry_points = len(entry.user_like.all()) - len(entry.user_dislike.all())
    entry.save()```
brave mantle
#

Python functions return None implicitly

deep cave
#

if you choose not to, python will return None

#

implic... yeah

brave mantle
#

:>

brittle copper
#

ValueError: The view mainsite.views.like didn't return an HttpResponse object. It returned None instead.

brave mantle
#

Ah, yeah, return a response then

deep cave
#

well that's Django telling you it has special rules. :D

brave mantle
#

django!

#

strider.

deep cave
#

strider?

brave mantle
#

I keep thinking of that GDQ run

#

"We went down this pipe and now we're in africa. Strider."

deep cave
#

the strider run?

#

haha

#

yeah

#

that was a fun one

brave mantle
#

Yeah, :P

brittle copper
#

Lovely

#

One last thing guys

#

How do I get the javascript POST request in django

brave mantle
#

the same way you get any form or other POST

#

What are you actually POSTing? JSON?

brittle copper
#

request.POST.get("getnew", 246) just to see if it works I have this and it toggles post with id 246

#

My post part may be a little fucked up hang on

brave mantle
#

Since I imagine you could probably just POST like

brittle copper
#
function ajaxfeedbackhelper(x,y,z) {
    x[z].children[0].addEventListener("click", function () {
        var getnew = new XMLHttpRequest();
        if (x[z].classList.contains("like")) {
            getnew.open("POST", sitename + "feedback/like/");
            getnew.setRequestHeader('X-CSRFToken', cookies['csrftoken']);
        } else if (x[z].classList.contains("dislike")) {
            getnew.open("POST", sitename + "feedback/dislike/");
            getnew.setRequestHeader('X-CSRFToken', cookies['csrftoken']);
        }
        getnew.onload = function () {
            if (x[z].classList.contains("voted")) {
                x[z].classList.remove("voted")
            } else {
                if (y[z].classList.contains("voted")) {
                    x[z].classList.add("voted");
                    y[z].classList.remove("voted")
                } else {
                    x[z].classList.add("voted")
                }
            }
        };
        getnew.send(x[z].getAttribute("data-id"))
    });
}

function ajaxfeedback(feedback, affected) {
    for (var i=0; i<feedback.length; i++) {
        ajaxfeedbackhelper(feedback, affected, i)
    }
}

ajaxfeedback(ajaxlike, ajaxdislike);
ajaxfeedback(ajaxdislike, ajaxlike);```
brave mantle
#

"post=x"

#

ah you're literally just posting the ID

#

well in that case there's nothing to parse and you want the request body

brittle copper
#

sooo, request.POST ?

brave mantle
#

that's a multidict if I'm not mistaken

#

that won't be populated

#

you probably just want request.body

brittle copper
#

oh

brave mantle
#

why don't you print it?

brittle copper
#

how :o

brave mantle
#

print(request.body)?

brittle copper
#

I feel like I'm stupid I'm sorry but where do I do that exactly :( in shell?!

brave mantle
#

in your function

brittle copper
#

I'm lost, where would it print it

brave mantle
#

You know what print does, right?

brittle copper
#

Okay prints to the console, I'm sorry just didn't think it would print it there for some reason :(

#

Although, this does it :D

brave mantle
#

Good stuff

brittle copper
#

Fixes my problem :D

brave mantle
#

\o/

brittle copper
#

Thank you, gimme a hug 🤗

brave mantle
#

Bruh blobhug

brittle copper
#

Oh yours cuter :3

brave mantle
brittle copper
#

Ohh pepe :(

native tide
#

hey, like in Flask, return puts a string to a HTML document in function ?

brittle copper
#

You have to put it in one of the render functions before in django

grand badge
#

im using django 2.0 and want a button on my website to delete all objects of the specified model, how will i go about doing that?

#

pls @ me when u gat answer

brave mantle
#

@grand badge Please ask a better question

#

The question you asked appears to be asking for someone to write it for you

#

I could give an overview if you want though

grand badge
#

yes i would like that

brave mantle
#

More or less, you'll need a form with a button in it, or just a button that will submit using Javascript

grand badge
#

oookaaay

brave mantle
#

You'll need a route that will be submitted to

grand badge
#

okay

brave mantle
#

That route will then need to run the delete operation on your database, having done whatever authorization checks you need

grand badge
#

soooo i need to make a model form right?

brave mantle
#

As I don't use Django I can't give that many specifics, but I don't think you need anything special

#

Just a single form with a button in your HTML should be fine

grand badge
#

okay thx

brittle copper
#

Literally object.delete()

#

You just need to loop through the ones you want

brave mantle
#

That sounds inefficient, wouldn't you do that on the database?

#

They want to delete all the objects in the table

brittle copper
#

Okay, isn't it just Modelname.objects.all().delete()

grand badge
#

i tried that, but it never worked unfortunately

brittle copper
#

Then put it in a for loop

#

Anyway GDUDE, while I have you here

#

I was turning all my hardcoded urls to template url patterns with {%url "urlname" regex_group = somevariable%}, how do I do this for GET requests?

#

My pagers are hardcoded rn

#

Like {%url "urlname" regex_group = somevariable%}?page={{next_page}}

brave mantle
#

No idea, sorry

#

I don't use that feature and I'm not sure why someone would

brittle copper
#

What :o

#

How else am I supposed to work with paginators 🤔

deep cave
#

what do you mean "how do I do this for GET requests"

brittle copper
#

url(r'^(?P<title_text>[a-zA-Z0-9_]+)/$', views.title_page, name="titlepage") this is one of my url patterns

#

I'm using this as {%url "urlname" title_text=title.title_url%} in html

#

Everything is fine so far, but to add a paginator to titlepage

#

I need something like ?page=pagenumber

brave mantle
#

Django has a pagination system? That's kind of neat

brittle copper
#

Which I can't find a solution except hardcoding it into html

brave mantle
#

I've always done it myself

brittle copper
#

Yeah it is pretty easy :D

deep cave
#

hm, I see.

#

I have never actually done this but let me fetch a django book from my bookshelf and see if I can figure it out.

brittle copper
#

Oh, thank you :D

#

@brave mantle

instance = Paginator(list, item_per_page)
page_item_list = instance(page)```
#

This is pretty much it

deep cave
#

I'm thinking maybe you just make a second url pattern which includes the ?page=<page_number> in the expression

#

and then you call that one instead

#

is that an option?

#

@brittle copper

brittle copper
#

Hmm, I can always try

deep cave
#

might be a better way but I'm not finding it in my django books

brittle copper
#

Well, this works and if I don't want to use hard coded urls this is what I have to use for now, thanks :D

deep cave
#

yeah.. I think you're stuck with that.

#

but at least it works

#

btw this book is excellent

brittle copper
#

Too bad I have to restructure all the views again

deep cave
#

I highly recommend it if you're gonna keep working with django

brittle copper
#

This is my life now, I try to fix something and all the sudden I have to do 10 hours of manual labour :D

#

Alrighty, will look at it when come across another problem

deep cave
#

it's not online or anything, you'd have to actually buy it

#

but it's not really for solving problems, rather for learning best practices and methodology

#

but it's really well written

brittle copper
#

Just clicked at link, yeah; probably can't afford it rn :D

deep cave
#

yeah, that's fair enough

#

just figured I'd mention it just in case you want a django book in the future.

brittle copper
#

Although, bookmarked it

brave mantle
#

Aren't we at Django 2.x though? :P

deep cave
#

django 2 exists, but I think 1.11 is still LTS

brittle copper
deep cave
#

oh wait, no look at that.

#

yeah actually the book I have is for django 1.8

#

I guess maybe there's no two scoops for django 2 yet.

brittle copper
#

Just found it, django itself too is saying "fuck you your urls have to be ugly"

raw rapids
#

how do i use requests to inject js into pages

frigid hamlet
#

you mean response?

#

forgot how, but you can work with static files@raw rapids

#

may be js script can be added to page as static object

#

but it headpain, before cofigure once

#

@raw rapids try just use "safe" flag in template variable, may be it will be enought, if js was generated on server side

#

inside django template html file variable syntax

#

if i right remember it something like html {safe|myvar}

#

where myvar is html <script>...</script> js code

brittle copper
#

How is that even related to requests library @frigid hamlet

frigid hamlet
#

may be i wrong translate... for me this text look like request info about ways to using django webframework that inject js code in response html page.@brittle copper

brittle copper
#

how do i use requests to inject js into pages this?

#

:o

frigid hamlet
#

like python server generate jscode and then transport this code use response into html.... yes , this text

#

i use this once... in django project..., that inject threejs 3d model js code into js template inside html, but forgot how :|

#

and lazy to search and read code again

brittle copper
#

You can just use javascript files in static folder and reference them from your template html

neat nest
#

if he's talking about actual "JS injection" then I think he's hard pressed to do such a thing with a library that doesn't execute JavaScript

brave mantle
kind steppe
#

good

brave mantle
#

Yep

cyan pilot
#

agreed

verbal chasm
#

amazing to finally see this!

ripe grotto
#

What would you consider when making an API: Django or Flask?

#

Or maybe another toolset?

neat nest
#

an API to compliment an existing application?

#

RESTful, in theory?

ripe grotto
#

RESTful, yup

#

And yes, to complement an existing application

neat nest
#

Flask is a pretty standard choice

#

I don't know that I've ever seen Django pulled in exclusively for use as an API

ripe grotto
#

Yeah it's just that I'm more comfortable with Django right now as I'm still learning

#

But flask seems very simple and easier than Django actually

neat nest
#

it's pretty lightweight and generally used for beginners and simple tasks

#

I haven't used it in... maybe 3 years

#

my last opinion on it was that the way I was taught to use it was not terribly pythonic, but when used in a more reasonable fashion it actually starts to look and feel a bit more like django

#

(I might also just be inventing memories, take it for what it's worth)

ripe grotto
#

3 years? That's a lot, what's your framework or library of preference?

neat nest
#

haven't been writing web apps in Python in 3 years! :P

#

Flask is probably the one I've had the most experience with, so I'd probably default to it unless I had time to research alternatives

ripe grotto
#

Oh, you don't do webapps then? Or maybe in another language?

neat nest
#

last web application I wrote was a quick PHP prototype, I think

#

toying with converting some network storage into web accessible cloud storage for the sysadmin team I was with

#

but in general, yeah, not working with webapps

ripe grotto
#

Like an OwnCloud sort of app?

neat nest
#

exactly like!

#

we actually tried to fit OwnCloud and NextCloud to our needs afterward

deep cave
#

owncloud is pretty damn good.

#

and yeah, I'd like to second the suggestion that flask is a better choice than django for a restful api.

#

styled is right that django and flask can be made to look similar, if you use class-based views and an ORM database with flask and split the codebase over lots of files and folders. but with Flask you have the option of writing your restful api in a single file with decorated function-based views. you can use a lightweight NoSQL database. it's all very self-contained.

#

in a word, Flask is less opinionated than Django.

#

solve the problem with however much complexity you require.

meager anchor
#

on the topic of Django, is there a way to call a template filter after rendering the template somehow?

#

specifically I have a blog which uses Markdown to render the posts, and would like to add a live preview to the admin

#

the markdown gets rendered through a template filter, or template tag, I forgot the exact naming

#

would I have to set up an endpoint on my site that does nothing but render the markdown given to it, and then call that endpoint from the admin?

#

god i have no idea of web dev pepe I think I need to do that though, judging from the responses on SO at least

deep cave
#

well, you could probably solve it with a bit of javascript

#

and have the preview be truly live.

#

I think that's what I would do.

#

without javascript, you will have to call some endpoint, yes.

#

I mean you could probably roll this preview stuff into the same endpoint you're already on at that point by spitting in an optional arg or something, but I'm not sure it'd be better.

meager anchor
#

"solve it with a bit of javascript" as in parsing the markdown in JS?

deep cave
#

that's one option

#

you could also have javascript dynamically load in whatever you're using for markdown already

#

as a daft example, javascript might make an ajax call and fetch the html from an endpoint you made to do this, and then just stick it on the page.

#

although it might be faster to just have a js markdown library handle it

#

(I'm sure there are a bunch)

#

I mean, there's probably a hundred ways to solve this with js. I'm not sure what the best method would be for your project, you'll probably have to do some research for that.

#

but for this kind of thing - a post preview - it's probably the way to go.

#

you don't wanna be reloading the page every time the preview is gonna change.

meager anchor
#

yeah

#

i'll probably go with fetching it from an endpoint, since I don't want to get into any weird issues because of library differences

#

thanks!

deep cave
#

np, let us know how you solve it

#

pretty interesting stuff.

meager anchor
#

will do 👍

mental chasm
#

@meager anchor When you say you are using an endpoint, are you re-rendering the markdown everytime they type a character??

meager anchor
#

uh

#

😃 I'll have to think of something smart

#

probably something along the lines of "if nothing was entered within the past x seconds, submit a request to grab the rendered output"

mental chasm
#

A lot of people use Showdown

#

If its on the client it means you arent having to send a HTTP request for every character

meager anchor
#

Yeah

#

Hm, maybe the whole "render the markdown on the server" approach is a bit stupid in the first place when i could just render it on the client

mental chasm
#

Yea

brittle copper
#

Isn't there any easy way to share stuff on facebook?

#

Facebook sharer requires an app id

#

My twitter sharer looks like this for reference

#
def sharetwitter(self):
    entry_title = self.entry_title.title_text.replace(' ','+')
    url = URL.replace(':','%3a').replace('/','%2f')
    pk = self.pk
    hashtags = ','.join(str(i) for i in self.entry_title.title_channels.all())
    return f"https://twitter.com/intent/tweet?text={entry_title}%3a+{url}entry%2f{pk}%2f&hashtags={hashtags}"```
meager anchor
#

pretty sure there's functions in the standard library to do the URL encoding

brittle copper
#

I'm pretty sure my 800 line views.py can be written in 400-500 lines

meager anchor
#

800 lines GWfroggyBlobSweat

brittle copper
#

:'(

#

So, about the facebook share? Anything? :'(

meager anchor
#

well it doesnt look that bad to me to be honest

#

apart from the url encoding

brittle copper
#

This is twitter though

mental chasm
#

Thats twitter

brittle copper
#

Can't do the facebook one :(

meager anchor
#

ahh

meager anchor
#

Trying to get client-side markdown rendering with highlighting working reminds me why I rendered it on the server in the first place

#

either I don't get any exception and it doesnt work, an exception that doesnt make sense, or something else GWjustinAngryThonk oh well, back to server side rendering..

mental chasm
#

:(

neat nest
#

rendering on the server with AJAX calls?

meager anchor
#

yeah but, i, uh, changed my mind again

#

the only issue I have now is code highlighting, which .. should work at some point

mental chasm
#

lol

neat nest
#

the feature planning phase is always a rollercoaster

#

wheeeeeeee

meager anchor
#

finally worked it out 🙏

#

I should probably work through FCC sometime.

mental chasm
#

huh?

meager anchor
#

you don't know fcc?

mental chasm
#

no

meager anchor
#

not this again

mental chasm
#

Oh I see

strange thorn
#

at which point would you say does caching make sense?

mental chasm
#

If the endpoint puts either a lot of heavy load, or gets hit a lot

#

Quite a vague answer I understand

neat nest
#

vague questions get vague answers

mental chasm
#

True

strange thorn
#

hmm

raw rapids
#

does anyone know how to add a proxy to scrapy?

kind steppe
#

essentially, setting the HTTP proxy environment variable will allow scrapy to use it

raw rapids
#

i understand you have to set it so its active but where do i add it when rnning the crawler

kind steppe
#

you set it before you run the crawler

#

I assume you are on windows?

raw rapids
#

mac

#

where do i set it

kind steppe
#

on mac or linux, you can run the export command before running your scraper

#

you're using Terminal to run?

raw rapids
#

yes

#

how do i run

kind steppe
#
export https_proxy=<proxy>
python3 scraper.py
raw rapids
#

i set the value of it to 1 right

kind steppe
#

no

raw rapids
#
'scrapy.downloadermiddlewares.httpproxy.HttpProxyMiddleware' : 1,
kind steppe
#

is that in a file?

#

if it's like a config file or something, then yeah

raw rapids
#

k

#

and if i do export do i oput that in a pthon file

kind steppe
#

No

#

You type that into your terminal and then run the scraper

#

like in the command I sent

#
export https_proxy=<proxy>
python3 scraper.py
raw rapids
#

but does it set the variable for the whole system

kind steppe
#

No, is that what you intend for it to do?

raw rapids
#

no

#

only for the scrapy crawler

#

but i run the crawler like this

kind steppe
#

Then it won't most likely

raw rapids
#

scrapy crawl <spider name>

kind steppe
#

then you just do

#
export https_proxy=<proxy>
scrapy crawl <spider name>
raw rapids
#

how will i know if its working

kind steppe
#

I've never used scrapy, I wouldn't know, I'm just going by what the docs say on using a proxy

meager anchor
#

Alright, I've integrated a live markdown preview into the Django Admin with some help of @mental chasm since i'm horrible with JS GWfroggyBlobThonk - this is what it looks like:

#

it might possibly be nicer to have the preview to the right of the content but i have no idea how to do that without grabbing yet another css framework think it's okay like this

mental chasm
#

Looks good

deep cave
#

I think that looks pretty good.

meager anchor
#

Thanks!

long swan
#

<div class='container'> <div class='row'> <div class='large-6'>Code content</div> <div class='large-6'>Preview Content</div> </div> </div>

#

If you are using bootstrap, maybe you could get markdown to implement this ¯_(ツ)_/¯

meager anchor
#

i'm not, it's the default django css, but thanks

raw rapids
#

Can anyone who knows how to use requests dm me I need hel

#

Help

brave mantle
native tide
#

hey guys

#

my code works but it's not getting all the fields I want.. I think it has something to do with the source code in html..
span or something.. so it's missing out certain fields

#
# LIST OF FIELDS TO SCRAPE
list_of_fields = ['Market Cap', 'Enterprise Value', 'Trailing P/E', 'Forward P/E', 'PEG Ratio', 'Price/Sales', 'Price/Book', 'Enterprise Value/Revenue', 'Enterprise Value/EBITDA', 'Fiscal Year Ends', 'Most Recent Quarter', 'Profit Margin', 'Operating Margin', 'Return on Assets', 'Return on Equity', 'Revenue', 'Revenue Per Share', 'Quarterly Revenue Growth', 'Gross Profit', 'EBITDA', 'Net Income Avi to Common', 'Diluted EPS', 'Quarterly Earnings Growth', 'Total Cash', 'Total Cash Per Share', 'Total Debt', 'Total Debt/Equity', 'Current Ratio', 'Book Value Per Share', 'Operating Cash Flow', 'Levered Free Cash Flow', 'Beta', '52-Week Change', 'S&amp;P500 52-Week Change', '52 Week High', '52 Week Low', '50-Day Moving Average', '200-Day Moving Average', 'Avg Vol (3 month)', 'Avg Vol (10 day)', 'Shares Outstanding', 'Float', '% Held by Insiders', '% Held by Institutions', 'Shares Short', 'Short Ratio', 'Short % of Float', 'Shares Short (prior month)', 'Forward Annual Dividend Rate', 'Forward Annual Dividend Yield', 'Trailing Annual Dividend Rate', 'Trailing Annual Dividend Yield', '5 Year Average Dividend Yield', 'Payout Ratio', 'Dividend Date', 'Ex-Dividend Date', 'Last Split Factor', 'Last Split Date']

#stock_ticket = 'TEF.MC'
sourceCode = str(urllib.request.urlopen('https://finance.yahoo.com/quote/TEF.MC/key-statistics?p=TEF.MC').read())
for field in list_of_fields:
    if(list_of_fields!=""):
        ScrapedValue = sourceCode.split('>' + field)[1].split('</td></tr>')[0].split('>')[-1]
        print ("field=",field, " / scraped valued= ", ScrapedValue) ```
#

for example.. fields like Fiscal year ends.... and Dividend date. .. are not being printed.. I think it's because it's in date format.. but I'm not sure how to correct it :< please help

deep prism
#

It's because a 'normal' field that you can parse looks like

<td class="Fz(s) Fw(500) Ta(end)" data-reactid="75">5.37</td>

And the fields you can't parse like Fiscal years looks like

<td class="Fz(s) Fw(500) Ta(end)" data-reactid="90"><span data-reactid="91">Dec 31, 2016</span></td>
#

Your code can't handle the extra <span> element.
@native tide

meager anchor
#

Not 100% sure if this is the right channel, but I'll ask anyway..
I have a Discord / Reddit / Twitch Bot for subscribing / unsubscribing to Twitch stream events through commands. Twitch handles this by sending callbacks to my site, which has to respond accordingly.
If they send me an event, e.g. "stream x went online", I need to access the database of the Bot to find out which guilds and which subreddits are following the stream for events and then do either

  • send the update to a webhook on discord
  • update the sidebar of a subreddit
    Now to my question, in django, is there a way to access a database from another project like that, or am I taking the wrong approach? I mean, I could also just import sqlite and then do the querying myself, but this all seems like bad practice. Would be happy for any suggestions
brave mantle
#

@meager anchor First of all: Don't use sqlite

#

Second of all, the database should be on the site - the bot should access it via a HTTP API

meager anchor
#

what's wrong with SQLite?

#

also hm okay

brave mantle
#

poor concurrency and speed

meager anchor
#

I only have like 2 guilds actively using this

brave mantle
#

Your site isn't going to be so forgiving

meager anchor
#

🤔

brave mantle
meager anchor
brave mantle
#

sqlite is only good for testing

meager anchor
#

I think I'll just go with the hacky import-sqlite-way

brave mantle
#

You're going to actually break it if you do that

meager anchor
#

Why

brave mantle
#

You've got a file-based DB and more than one process accessing it

#

you're gonna corrupt it

meager anchor
#

goddamnit

#

"Multiple processes can have the same database open at the same time. Multiple processes can be doing a SELECT at the same time. But only one process can be making changes to the database at any moment in time, however."

#

the site doesnt make changes

brave mantle
#

Yeah, but you can't guarantee that the reads will be atomic

#

sqlite isn't writing as soon as you make a change, and you could be reading during a write as well

meager anchor
#

if it breaks I'll learn from it, and if not I'll save a couple hours of rewriting half my bot and my website to have an API GWfroggyWeSmart

brave mantle
#

the couple hours is worth it when you're learning to do things the right way :P

meager anchor
#

i've had to rewrite half my bot twice already because twitch moved to their new and amazing API, and all for the one guild that uses it

brave mantle
#

Sounds like you could use some modularity, but yeah, that happens

meager anchor
#

probably

deep cave
#

it'a pretty quick to switch out your sqlite for postgres or something.

#

which is easy to make available to all your services.

meager anchor
#

yeah

#

well, I just figured out that twitch doesn't send the stream name / game name anymore, just IDs, so half of this is broken anyways

deep cave
#

you interact with both databases in almost exactly the same way, usually.

#

at least if you're just sending sql queries to sqllite with execute or something. psycopg2 will be identical.

#

just gotta change the import.

#

and a remotely accessible db is a nice thing to have. for future projects.

meager anchor
#

i'm using Django ORM in my web app and SQLite ORM in my bot, so its no issue at all

#

probably just changing a connection string

deep cave
#

right right.

#

yeah that should be pretty simple

#

so I think gdude is right, you should just get a database that supports this.

meager anchor
#

yeah

#

well I need to integrate the twitch app into my django web app somehow now, so I'll have it in two projects...

#

I think I'll just go with adding an API to my site

mental chasm
#

@brave mantle

🅶🅳🆄🅳🅴 - Yesterday at 11:15
sqlite is only good for testing```
#

Well that just isnt true

brave mantle
#

Oh, well sure, if you're making a small application that you're only ever gonna use yourself, or you're locking yourself into a model with no concurrency, sure

mental chasm
#

Are you high?

#

There are quite a few use cases

brittle copper
#

I'm high but continue

cyan pilot
#

what's wrong with sqlite for small-scale stuff?

mental chasm
#

Nothing at all

brittle copper
#

What's wrong with sqlite for bigger stuff?

#

I'm not kidding around I really don't know :(

mental chasm
#

Only one write can happen at one time

#

So you would need to implement some sort of queue system

#

Which if you have a high volume of requests, may not be what you want

brave mantle
#

file-based sqlite is known to be pretty slow, as well

mental chasm
#

Slow compared to what?

brave mantle
#

slow compared to most anything with its own daemon, really

#

in-memory sqlite is okay I guess, but I'm not sure of the use-cases of that

mental chasm
#

That is a pretty bold claim

#

Considering SQLite doesnt have the network overhead i doubt there is much truth in that

cyan pilot
#

I was going to use sqlite for trading card thing, but decided against it due to not really feeling like writing a parser for moving things into a dict

mental chasm
#

What?

cyan pilot
#

unrelated

mental chasm
#

But i still dont understand the last part

cyan pilot
#

I'm lazy.

#

I would use sqlite otherwise

#

but I'm using pickle instead

mental chasm
#

Do you mean because SQLite returns a tuple?

cyan pilot
#

the dicts I'm using are nested, pickle is just more convenient.

#

Regardless, I will try to make it work later when I finish up the actual content of the project.

mental chasm
#

Ok

cyan pilot
#

My fault for derailing an argument on the practicality of sqlite3

#

but really it boils down to how frequently the database is accessed

#

Running a bot for a personal server would be fine, but a multi-server bot like tatsumaki might need something better

#

(just adding a tl;dr for anyone who doesn't care to scroll up)

nocturne plover
#

It's sqlite is probably not going to be your bottleneck for a discord bot of any size.

brave mantle
#

Once you get past 1000 servers, it'll be unusable unless you're using a beefy enough rig for d.py's AutoShardedBot

#

at 1k servers, Discord forces you to shard

#

The "correct" way to do it is to split off into multiple processes, but asyncio is generally pretty fast

nocturne plover
#

oh, for multiple instances? You have to implement c/s for the bot anyway.

#

No reason to compare them at that point if you have to stand up a server anyway.

brave mantle
#

c/s?

nocturne plover
#

client/server

brave mantle
#

Oh, if you've got more than one instance, yeah, for sure

#

unless you wanted to be silly and make like

#

an sqlite server daemon

#

which I'm sure exists, tbh

nocturne plover
#

I LOVE NFS AND FILE LOCKING

#

I mean what

brave mantle
#

lol

nocturne plover
#

(No really that's the solution for some software that I deploy at an event each year.)

#

(These motherfuckers all access a single database file on a network share using a lock file.)

#

(homemade format database*)

brave mantle
#

Yikes

nocturne plover
#

Used samba one year to do it so I could script inotify to make a copy after each write mtime change just in case

#

Was scary D:

#

Also, bananas are a neat fruit

brave mantle
#

At least they weren't using shelve, I suppose :P

nocturne plover
#

I'm going to assume that not having heard of shelve is a good thing ;p

brave mantle
#

It's one of those python modules you only see beginners using

#

It's a key value store that is serialised with pickle and has no querying aside from the keys

#

Single file of course

nocturne plover
#

I have written this module at some point

#

data_store = {} 😂

brave mantle
#

:P

native tide
#

lmfao

opal coral
#

ok so, i have no clue what to use as a backend for my nonexistant website

strange thorn
#

flask flask flask

opal coral
#

but google told me that its single-threaded

strange thorn
#

but you can do wsgi with gunicorn or some other webserver OR enable the multi threading option?

opal coral
#

ok so google tells me theres single threaded, but not that theres a multithreading option?

#

how did i not know about this

strange thorn
#

huh

#

i read about a multi threading option

#

and flask is made for wsgi anyways

#

google gunicorn with flask

opal coral
#

im gonna be honest with you i have no idea what wsgi or gunicorn is

#

all i want is like a do this and this when someone goes to /home and give them this page

#

so like flask, but not one thread

brave mantle
#

wsgi is inherently not compatible with asynchronous programming

#

however

#

what you usually do is use a worker manager, like gunicorn

#

so you're actually running like 24 copies of your flask app

#

and it dispatches requests to whichever ones aren't busy

opal coral
#

ah ok gunicorn sounds useful

#

can you make it dynamically spin up more workers if the load increases (though i doubt my website will get that much attention)

brave mantle
#

I think so, but you probably don't need it

#

With flask, you can combine it with gevent as well, which is what we're doing with the python discord site

#

and gunicorn will handle most of that as well

opal coral
#

so we're using gevent, gunicorn, and flask?

brave mantle
#

Yep

opal coral
#

ahhh ok

#

i will look into all of those things

#

thanks for the advice!

brittle copper
#

Just use Django like a real man

nocturne plover
#

Flask love

strange thorn
#

^

kind steppe
#

flask + gunicorn + nginx + gevent best

strange thorn
#

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#

now we just need some nice db

#

and its perfect

kind steppe
#
  • Cassandra yoj
strange thorn
#

joseph will say rethink now

#

oh

kind steppe
#

I prefer cassandra

strange thorn
#

yeah cassandra is quite nice

#

especially for larger scale stuff

kind steppe
#

I mean it's only for larger scale stuff lmao

strange thorn
#

you can just keep adding and adding and dding nodes

kind steppe
#

running it for anything smaller is overkill

#

yeah

#

However the issue is

#

"While Cassandra can be made to run on small servers for testing or development environments (including Raspberry Pis), a minimal production server requires at least 2 cores, and at least 8GB of RAM. Typical production servers have 8 or more cores and at least 32GB of RAM."

#

I have none of those

#

so when I want to mess around with cassandra

#

I have to fire up a cassandra node at my work

#

and start messing with it from home

#

which is slow

strange thorn
#

you work?

#

how old are you?

kind steppe
#

in my teens

#

We can get sorta jobs here

#

starting at 14

strange thorn
#

dafuq

kind steppe
#

ikr

strange thorn
#

we are allowed to get jobs with 14 and then just like minimal paid jobs

#

so we are again back to

#

I NEED TO AGE FASTER

kind steppe
cyan pilot
#

i ceo i 12

kind steppe
#

henlo am 2 years of age i am ceo of my friends minecraft server

strange thorn
#

😃

cyan pilot
#

😄

kind steppe
#

lmao

#

what

strange thorn
#

ok so we ARE allowed to work once we are 13

#

for 2h a day

kind steppe
#

when a linux install loads dll's onto your machine

strange thorn
#

when we are 16 we may accept jobs full time and work for four weeks a year

#

(if we are still in school during that you CAN leave school with 15 and work 8 hours a day)

#

and how much may you work?

#

@kind steppe

kind steppe
#

I work 4h on weekends

strange thorn
#

he

#

working on weekend in not hospital or hotel job

#

illegal here

kind steppe
#

lmao

strange thorn
#

(for kids)

kind steppe
#

sad

#

hey i know what would be fun

#

running a cassandra node on my laptop

strange thorn
#

could get interesting

kind steppe
#

okay so

#

the command to start it told me it took too long to start

#

and to check the status

#

the status check command timed out and told me it may not be started

#

cassandra are you lying

strange thorn
#

no cassandra is always saying the truth

kind steppe
#

o

strange thorn
#

maybe

#

YOU are lying 😱

kind steppe
#

yes

#

lmao what

#

it started but then exited a few seconds later

#

and then the log dir is empty

#

nevermind cassandra got pissy at java

strange thorn
#

😄

kind steppe
#

this was a bad move

strange thorn
#

what happened?

kind steppe
#

laptop ram spiked to 100 and laptop froze

strange thorn
#

🤣

kind steppe
#

remotely killed it

#

it still appears to be running

strange thorn
#

did even the ttys freeze

kind steppe
#

yes

#

it is

#

yes they did

strange thorn
#

what

#

thats like

#

nearly impossible

kind steppe
#

not when you have 100% RAM & CPU usage

#
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address    Load       Tokens       Owns (effective)  Host ID                               Rack
UN  127.0.0.1  103.69 KiB  256          100.0%            a922cfab-b51e-4462-ac9a-79ab236d56fa  rack1



#

huh okay

#

it's still running

#

wat

strange thorn
#

Datacenter: datacenter1

??

kind steppe
#

yeah i'm using the default install stuff

#

it auto assigns a datacenter and rack

#

in prod I could say "oh yeah it's in us-west and on rack 12

#

so if there was an issue with it

#

I could go fix that node

#
cqlsh> SELECT key, broadcast_address, rack, cql_version FROM system.local;

 key   | broadcast_address | rack  | cql_version
-------+-------------------+-------+-------------
 local |         127.0.0.1 | rack1 |       3.4.4

(1 rows)

#

lmao what

strange thorn
#

but

#

if cassandra is still running

#

what claimed so much ram then

kind steppe
#

startup of cassandra i'd assume

#

a nice feature of CQLSH is that you can open up doc links from inside the interface

#

help INSERT_JSON opens up the inserting json docs

#

only issue is it links to 404 documents lmao

#

luckily the directory is indexable so you can find things in the end

#

this is nice tho

#
INSERT INTO mytable JSON '{"\"myKey\"": 0, "value": 0}'
#

json inserting

#

for some reason this is working

#

so from some quick benchmarks everything seems good

#

jus a high cpu usage lol

#

i never knew about the json insert so i was messing with it kek

nocturne plover
#

@kind steppe gunicorn?

#

You meant: uwsgi

kind steppe
#

wat

#

no

nocturne plover
#

yissss

#

is beautiful

kind steppe
#

no

#

u

stone pelican
#

Does anyone have any idea, how flask's Request object, changes, depending on what function it's in? For example, if I register a callback for /home/, how can the Request object change, depending on the request for /home/ that was made?

stone pelican
#

If anyone knows what I mean by that :p

brave mantle
#

the request object is a thread local

#

That's what you need to research :P

stone pelican
#

Alrighty

indigo holly
#

Looking for some help on a project, I'm new to python for WD and was given some JS and CSS by work to design a site.

I'm using Flask. I don't have any HTML or JS background so I'm looking for a second set of eyes to see if there's something basic I'm mixing up.

My issue is the flask site is supposed to pull information from csv files and display them as a graph, however I get the GET 404 error of the csv file when I try. Here is some of the code:

brittle copper
#

@indigo holly Your .csv file in not in your static dir

indigo holly
#

Wow, such a silly mistake. Thanks

brittle copper
#

Anytime :D

formal junco
#

Hey guys, I'm new and learning Python. Kinda stuck, have learned the basics and not sure where to go next. I'm interested in learning some web dev stuff, any recommendations? I feel like I have a good grasp of how to structure functions and code, but have no idea about how to arrange files or develop a website.

neat nest
#

small, simple, extremely common and well-documented

formal junco
#

Cool, thanks. Do you think Flask is a better start than Django?

stone pelican
#

yep

neat nest
#

if you're just getting out of the basics and haven't touched web dev before? absolutely

formal junco
#

Will check it out

rose quest
#

I've generally seen django espoused as the more powerful option but

#

Generally beginners won't need the shiniest chrome and biggest engine and raddest rides

formal junco
#

I gotcha.

deep cave
#

I actually don't subscribe to the point of view that django is more powerful.

#

it's just more opinionated.

formal junco
#

the gist I seem to get from others is that django makes more executive decisions whereas flask gives more freedom

#

not that I know too much about any of this

deep cave
#

yeah, that's what I mean by opinionated

#

you can use a document-oriented database like mongo or rethinkdb with django if you absolutely want to

#

but it sure doesn't make it easy for you

#

it reaaally wants you to use a SQL database.

#

flask, on the other hand, let's you choose freely and has no bias towards one or the other.

#

and this is a recurring theme when comparing the two.

rose quest
#

@deep cave I am by no means a webdev so I don't have any strong opinions one way or the other

deep cave
#

that's okay, you're entirely correct of course that django is espoused as the powerful option.

#

I acknowledge that it is. but as someone who works with python and web, I don't subscribe to that point of view. just figured I'd put that out there. :)

formal junco
#

flask seems to be a lot simpler also

deep cave
#

it can be. a single file is all you need.

#

django requires a whole hierarchy of files.

#

but, to be clear, it can also be just as complex as django.

#

depends what you're doing.

ripe grotto
#

Hello, I know this is a very newbie question but, are patch, delete and put relevant HTTP methods?

#

I know get, head and post are, but I haven't seen real world examples with patch, put or delete

deep cave
#

PUT and DELETE are valid HTTP methods

#

I've never actually heard of PATCH being one.. ?

#

pretty sure it isn't

ripe grotto
#

Yeah, they are, I now put and delete are http methods, but my question is: are they relevant methods in real world escenarios?

neat nest
#

there's actually a number that don't bump around much anymore https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods

deep cave
#

I think DELETE is useful.

#

I use it when I write restful API's that interact with databases to handle removing stuff.

#

I use PUT for like.. uploading files and the likes.

#

you could technically just use POST for that too, but it feels wrong.

#

I mean, you could use POST for everything, but the point is to have some seperation

#

none of them are strictly speaking useful in the sense of "do you absolutely need to use this because it has a special feature"

#

I will say that I very rarely use OPTIONS, though.

ripe grotto
#

Oh, ok so: POST, PUT, GET and DELETE are the most used methods then?

deep cave
#

and yeah, it seems PATCH is a thing

#

yep, without a doubt.

#

those are all fairly common to see.

#

PUT a little less so than the others.

#

POST and GET extremely common.

ripe grotto
#

gotcha, thanks @deep cave 👌🏻

brittle copper
#

I only use POST and GET

#

I don't know what the others are and whether if they can do things that POST can't do

#

Since after POST you can pretty much do anything with that info at the backend

tough raptor
#

You can accept whatever you want. You should consider accepting others, as they can be used to convey the semantics of your api

#

That's espe true if you're making an api. Not so much if you're making a site

ripe grotto
#

But be aware, that PUT is highly exploitable, so be sure to add a good authentication method before working with PUT

tough raptor
#

In what way is PUT more exploitable than other methods?

ripe grotto
#

PUT can directly mess with local files, someone can write, lets say, a PHP file in your server and enable code execution

#

uploading a web shell, etc.

tough raptor
#

Only if you have a server with that set up

ripe grotto
#

Yeah, good practices mitigate a lot of vulnerabilities like this, if your server is correctly configured, and has a good authentication system then it won't be vulnerable

tough raptor
#

Just don't have put write local files at all

#

If you're building a web backend you're in control of that

#

It's not like adding a put endpoint in flask suddenly enables people to write local files

ripe grotto
#

I know, but a lot of sysadmins and web developers are just lazy, leave services with their default settings and believe me, there are a LOT of http services with PUT enabled to everyone 🤣

tough raptor
#

Ok, your experience is different to mine. I've never worked with a server where there was a PUT endpoint, enabled or not, that just wrote local files

ripe grotto
#

Congratulations

cyan pilot
#

Is requests THAT good?

neat nest
#

I have a feeling Daniel Greenfeld was doing something horribly wrong

cyan pilot
#

He might not have written the original code.

cyan pilot
#

wow

#

I already know so much about requests

brittle copper
#

How can you even write something in 1200 lines while it can be done in 10

#

Like

cyan pilot
#

OOP

brittle copper
#

Do you write python code to turn your code into c then compiles it which then open a python terminal and does the necessary 10 lines of code?

#

Even that wouldn't be 1200

cyan pilot
#

He never really spcified what it was

west glen
cyan pilot
#

I've seen that

#

hence my jab at OOP

brittle copper
#

Is that even real

#

I'm trying to imagine what one may have wrote in those 600 lines

native tide
#

it's quite easy to do actually

#

especially if you follow OOP standards hard

#

you kinda have to step back and look at how people want to use your library and what you're actually providing

#

and always think about the cost of fracturing your code

cyan pilot
#

It's official, I can now use requests pretty okay

#

but I can't show what I did with it

ripe grotto
#

Why not?

brave mantle
#

NDA probably

ripe grotto
#

National defense academy?

brave mantle
#

non-disclosure agreement

ripe grotto
#

I love requests

brave mantle
#

Lots of contracts require signing one of those these days, especially in IT

ripe grotto
mild bridge
#

requests and beautifulsoup are a great combo

#

10/10

#

throw in some selenium

cyan pilot
#

actually I can't show what I did because it's lewd

olive oxide
#

owo?

cyan pilot
#

but if you really want to see you can dm me

olive oxide
#

depends on the type of lood we're taaaalkin

cyan pilot
#

uhhhhhhhhhhhhhhhhhhhhhhhhh

#

the e621 api is next

olive oxide
#

oh fooey that's hardly lood

cyan pilot