#web-development
2 messages Β· Page 225 of 1
So in flask, how could I return a page, then wait 3 seconds and then return another page?
Or would that be made in pure js
after you return a response, you can't change it. You'll need to use javascript
setTimeout(() => {
window.location = "/newlocation";
}, 3000)
Anyone here with expertise in Django ???:
Hi guys, I'm new to web dev and I'm building a portfolio website with Django. I'm not good with frontend.. are there any free resources that I can use like templates to make the html/css look nice?
You can take a look at https://html5up.net/
is anyone aware of an asyncio-friendly password hashing library? Ideally something that supports argon2 that can be awaited
passlib seems to be the go-to but I can't find anything suggesting it supports asyncio natively.
I'd like to avoid having to write my own async plumbing around it
I think you could try to use it with anyio:
https://anyio.readthedocs.io/en/stable/threads.html#running-a-function-in-a-worker-thread
Assuming passlib doesn't hold GIL π€
why not just drop your question instead of asking something like that.. you'll easily get help if u ask your question directly
why is it better to use a storage platform for storing databases, rather than just use the database on a vps like a local machine?
Scalability, but it doesnβt mean itβs always the better option to go with.
Yes
I donβt know are they the same thing ?
Then looks like a heroku problem.
Yes but these days itβs all automated through CI.
Hi everyone, Actually I'm working on Building a Blog web Application using Flask and the problem I faced is on adding a code snippet option to my Editor with Flask-CKeditor
The Editor load and there are no any problem but How to add a code snippest option:
What I did is I Download the CKEditor version 4.17 from here
https://ckeditor.com/ckeditor-4/download/
Then I Did the configration to load the CKeditor locally as below:
app.config['CKEDITOR_SERVE_LOCAL'] = True
and then I copy the ckeditor folder to static folder and load it with jinja as below where I want to use it on the web page.
{{ ckeditor.load(custom_url=url_for('static', filename='ckeditor/ckeditor.js')) }}
I download the code snippest plugin from here
https://download.ckeditor.com/codesnippet/releases/codesnippet_4.17.2.zip
Then I activated as Flask-CKeditor mentioned on their documentation like this
app.config['CKEDITOR_ENABLE_CODESNIPPET'] = True
app.config['CKEDITOR_CODE_THEME'] = 'school_book'
for full code here
https://github.com/Shalabyelectronics/BlogCapstoneProject
hello everyone, does anyone have any expereince with datatables from datatables.net?
Actually bro I haven't yet started with django!
if u want some resource, then u can checkout the youtube video of freecodecamp
it's a good one
imo
Ya dude thanks.
This video is a full backend web development course with python. In the course, you will learn everything you need to know to start your web development journey with Python and Django.
βοΈ Course developed by CodeWithTomi. Check out his channel: https://www.youtube.com/c/CodeWithTomi
π Join CodeWithTomi's Discord Server: https://discord.gg/cjqNB...
:")
Are you a working professional?
professional? eh.. no i don't think so
but i've used it
and i'm pretty much okay with it ig?
Ok
any idea why i cant put an image into the website? this is my code im basically trying to get a grasp on web dev
from flask import Flask, render_template, url_for
app = Flask(__name__)
@app.route("/")
def home():
return render_template("index.html")
if __name__ == "__main__":
app.run(debug=True)
this is the index.html code
<!DOCTYPE html>
<html>
<head>
<title>Learning html baby</title>
<style>
img {
width: 100px;
}
</style>
</head>
<body>
<img src="SiteTests\Images\assassin.jpg">
</body>
</html>
just trying to experiment with this and for some reason its just not working
hey guys how can i make the photo and text in same line?
i saw a stackoverflow question mentioning the exact same topic, tried using the answer, and it just still doesnt work for me, although something interesting, if i right click on this small little icon in the website
and i click save as
it actually saves the thingy on a .html file and the pic is displayed correctly if i open that .html file
use url_for
i'm having trouble understading what RESTful APIs are. Are they just JSON data that is easily accessible over the internet and retrievable through python?
REST is a set of guidelines for designing api's, RESTful API is an API that uses REST
api can use json but not use rest at the same time
Also iirc REST doesn't mention JSON at all, so you can use any format you like
mm okay, ill do some research about it, thanks in advance
REST stands for "Representational State Transfer" which is a fancy way of saying "URLs are nouns, HTTP actions are verbs"
I see. So basically we can manipulate or interact with REST APIs via GET, POST, PUT, DELETE requests?
yes
so a GET to /users is expected to return a list of user objects, while a POST to /users is expected to create a new user
PATCH to /users/<id> is expected to partially update an existing user, etc
you'll sometimes hear them referred to as CRUD actions
create, read, update, delete
applies for images as well
hmm so REST APIs are CRUD apps?
or i mean you can use REST APIs for CRUD apps
or REST APIs are meant for CRUD apps
you can use them for that
I guess REST APIs can be used for a lot of things and I'm just not getting the full scope of it
REST is just a structure for an API
You rarely can do with just CRUD, but in most cases you have some sort of entities and CRUD is preferred way to deal with them
But you for example might have endpoints like this:
/auth/sign-up
/auth/sign-in
/transactions/statistics
in dogmatic REST there's only HTTP verbs on URL nouns. In reality you'll have the occasional "do the thing" URLs like Doctor details above
Yep, but when possible it's better to use nouns in urls
@amber ember You solved your issue with passlib?
I was just doing some research. Not actual coding yet
want to eventually graft something in to a FastAPI back end
so i can have an API endpoint like.. blog/register/
you can technically have any API endpoint you want π
which register doesn't really pertain to any of the CRUD actions. i guess POST?
if you're gonna write a blog, you'll probably have posts and comments as your main objects
so like...posts/<id>/comments
hmm i guess what i'm referring to is like.. for posts/<id>/comments uses the create action of CRUD to create a comment?
or update if the user edits it
i guess the end action
i don't know if i'm thinking about it the right way though
GET /posts - return a list of posts
GET /post/<id> - return an individual post
POST /posts - create a new post
PUT or PATCH /posts/<id> - update an existing post
DELETE /posts/<id> - delete a specific post
GET /posts/<id>/comments - get all comments on a specific post
GET /posts/<id>/comments/<id> - get a specific comment on a post
POST /posts/<id>/comments - create a new comment on a post
you may or may not want to let people update comments on posts but if you did it'd be a PUT or PATCH on /posts/<id>/comments/<id>
DELETE - /posts/<id>/comments/<id> - delete a comment from a post
PUT/PATCH are sometimes used interchangably as they both mean "update an object"
in precise terms PUT is "replace this object" and PATCH is "update this object"
but in practice most people aren't gonna care if you use PUT or PATCH for updating an object
ah so basically you'd have to make a function for each one of those, depending on how you want it
yep
Here's some pseudocode that vaguely represents FastAPI
from fastapi import FastAPI
app = FastAPI()
@app.get("/posts", response_model=list[PostViewModel])
def index_posts(db: ObjectThatRepresentsADatabaseConnection):
return get_posts_from_the_db(db)
@app.post("/posts", response_model=PostViewModel)
def create_post(db: ObjectThatRepresentsADatabaseConnection, post: ObjectThatContainsPostInformation):
return create_a_post_in_the_db(db, post)
@app.get("/posts/{id}", response_model=PostViewModel)
def show_post(db: ObjectThatRepresentsADatabaseConnection, id: int):
return get_post_from_db(db, id)
@app.patch("/posts/{id}", response_model=PostViewModel)
def update_post(db: ObjectThatRepresentsADatabaseConnection, id: int, updated_post: ObjectThatContainsPostInformation):
return update_post_in_db(db, id, updated_post)
@app.delete("/posts/{id}")
def delete_post(db: ObjectThatRepresentsADatabaseConnection, id: int):
return delete_post_from_db(db, id)
hmm is the FastAPI module necessary?
REST is language and framework agnostic
You'll want to use some sort of framework, be it FastAPI, Flask, Django/Django Rest Framework
You can use anything to build rest api's
but strictly speaking no, you could write it all from scratch
wouldn't recommend it though
ooh
Yep, i'd recommend FastAPI
Django is hard to customize and it forces you to use it's orm
django itself is a bit dated
i am using django to build this site. but i guess i could forego django's Rest API and use FastAPI isntead?
well if you're already using django then go with that
there's a HUGE amount of information out there to help you learn
django is an older framework but the ecosystem and community knowledge is massive
FastAPI is the new kid on the block, and is making waves. It's all I've written at my job for the last year
There's still nothing that would match fastapi, django rest framework is just harder to use
yeah but this person obviously is somewhat newish to this whole thing so if they're already using django I dunno what the benefit would be to them ditching it for FastAPI + some JS framework
I wish my team have chosen fastapi instead of django when we started our backend
if it's someone experienced then yeah, absolutely use FastAPI every time
It depends on how much progress you made with django and current project already
I just think FastAPI needs a little more time to get more ecosystem around it for people who are just learning this stuff, but that's just me
@amber ember To be honest i don't see any advantages in using django other than built-in authentication, maybe drf has some things like built-in filters and pagination, that's pretty much all
in this case the advantage is a hell of a lot more learning material to draw from
Django ORM is just a hindrance
true, although I work with plenty of people that curse the name of SQLAlchemy every time you bring it up π
Not many people could make their way through sqlalchemy documentation
I've been doing this for multiple decades now and even I struggle with that documentation
it's so goddamn dense
There's api reference and guides for using different parts of sqlalchemy, it covers most of the use cases
Does anyone have experience with deploying docker on digital-ocean? I've got a dockerfile for a Django application I'm trying to deploy that is stalling on build. This followed needing to add a RUN command RUN gunicorn --worker-tmp-dir /tmp Whurthy.wsgi
The central challenge is that there is something DO is doing with builds that doesn't default the tmp directory...which is why this RUN command is needed. I've tried the two following BUILDS
ENTRYPOINT ["gunicorn"]
CMD ["Whurthy.wsgi", "-b", "0.0.0.0:8000"]```
and
ENTRYPOINT [ "/bin/bash", "-l", "-c" ]
CMD ["gunicorn --workers $WORKERS --threads $THREADS --timeout $TIMEOUT --bind :$PORT run:app"]```
Both are failing. DO Support is not replying to this stall on build.
Can you share whole dockerfile?
FROM python:3.8-slim
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV HOME=/app
ENV APP_HOME=/app/web
RUN mkdir -p $APP_HOME && mkdir -p build/static && mkdir -p build/tmp
WORKDIR $APP_HOME
RUN apt-get -y update \
&& apt-get install -y \
gcc \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
COPY ./requirements.txt ./
RUN pip install -r requirements.txt psycopg2==2.9.3
COPY . .
RUN python3 manage.py collectstatic -c --noinput \
&& chmod 777 $APP_HOME
RUN gunicorn --worker-tmp-dir /tmp Whurthy.wsgi
EXPOSE 8000
ENTRYPOINT [ "/bin/bash", "-l", "-c" ]
CMD ["gunicorn --workers $WORKERS --threads $THREADS --timeout $TIMEOUT --bind :$PORT run:app"]```
Why do you need RUN gunicorn --worker-tmp-dir /tmp Whurthy.wsgi?
This works locally. However, DO is doing something that is reflected in comments around a known issue on this
Because of the aforementioned issue. DO even recommend the use of a RUN command here: https://docs.digitalocean.com/products/app-platform/languages-frameworks/python/django/
Specifically:
Modify the Run Command setting to point to your application. For this example my project is named mysite. So the modified command would be gunicorn --worker-tmp-dir /dev/shm mysite.wsgi.
So, without that run command the deploy fails to truly work. The known issue is the way DO processes the tmp directory during build.
What's the issue though? π€
The application build hangs
Hm, on which stage?
Which command in dockerfile?
That screenshot shows the live build log. All commands work. However, the build hangs
It successfully starts gunicorn
Because you're running your gunicorn during build
But something is NOT workign
So it never finishes
Ok. So what do i change?
Because this has been all day on this known issue.
The dockerfile works locally...but not when deploying to DO
I use this dockerfile for django at work:
FROM python:3.9-slim-bullseye
WORKDIR app
RUN pip install poetry==1.1.12
COPY ./poetry.lock ./pyproject.toml ./
RUN poetry run pip install 'setuptools==61.2.0'
RUN poetry install --no-dev
COPY . .
RUN poetry run python manage.py collectstatic --settings main.settings
ENTRYPOINT ["poetry", "run", "gunicorn", "--worker-class", "gevent", "--bind", "0.0.0.0:8000", "main.wsgi"]
yeah the gunicorn process never terminates
Just create a correct entrypoint, your build seems to be ok
remove RUN gunicorn ..., remove your ENTRYPOINT and CMD
Use entrypoint similar to this ^
But if I remove that RUN command I'm ignoring their documentation and back to the original issue
yeah but doing RUN gunicorn the process never terminates
Then why on earth are they saying to do that in their docs???
Send me the link please
point 7...
AND...support directly suggested adding that RUN
It's probably references entrypoint
Which is why I originally asked for experience with deploying dockerfiles with Digital Ocean
Also it doesn't mention docker at all
So your "run command" is the command you're starting gunicorn with
not RUN in dockerfile
No it doesn't, but support did
When I asked for clarification they didn't reply
I'm willing to bet they meant "CMD"
I really WANT this to work. Django deployment is ARSE
Entrypoint is preferred way
ENTRYPOINT ["poetry", "run", "gunicorn", "--worker-class", "gevent", "--bind", "0.0.0.0:8000", "main.wsgi"]
version: "3.9"
services:
backend:
build: .
command: ["--workers", "4"]
I can later add more arguments in docker-compose, docker swarm, k8s manifests or anywhere else
I thought entrypoint was the script that then becomes PID 1 and CMD is what was actually run by the entrypoint script
OK. Good learning...Digiital Ocean's documentation is ARSE
usually we use tini for entrypoint
You just interpreted it incorrectly
Fair.
I love to code...I really detest deployment
i'm not that far into it, and wouldn't remind starting for the practice. but do companies use Django ORM at all? Is it worth learning to put on my resume
\
Django ORM specifically doesn't matter, SQL knowledge would be more important
So I need to rewrite my dockerfile to allow for my application?
I currently have:
EXPOSE 8000
ENTRYPOINT ["gunicorn"]
CMD ["Whurthy.wsgi", "-b", "0.0.0.0:8000"]```
Just remove your RUN gunicorn ... and add --worker-tmp-dir /tmp to your entrypoint (or cmd)
like so ENTRYPOINT ["gunicorn --worker-tmp-dir /tmp"]
It's recommended to enclose each argument into string
ENTRYPOINT ["gunicorn", "--worker-tmp-dir /tmp"]
like this:
ENTRYPOINT ["poetry", "run", "gunicorn", "main.wsgi"]
hmm so doing raw SQL queries with python would be better? I guess it translates better if using different databases like PostgreSQL vs sqlite3 or something?
ENTRYPOINT ["gunicorn", "--worker-tmp-dir", "/tmp"]
No, in most cases you want to use orm, it makes your code easier to write and maintain, but to use ORM you'd still need sql knowledge
Excellent...so now I have:
FROM python:3.8-slim
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV HOME=/app
ENV APP_HOME=/app/web
RUN mkdir -p $APP_HOME && mkdir -p build/static && mkdir -p build/tmp
WORKDIR $APP_HOME
RUN apt-get -y update \
&& apt-get install -y \
gcc \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
COPY ./requirements.txt ./
RUN pip install -r requirements.txt psycopg2==2.9.3
COPY . .
RUN python3 manage.py collectstatic -c --noinput \
&& chmod 777 $APP_HOME
EXPOSE 8000
ENTRYPOINT ["gunicorn", "--worker-tmp-dir" ,"/tmp"]
CMD ["Whurthy.wsgi", "-b", "0.0.0.0:8000"]```
looks fine to me
Thank you very much π I shall now push and deploy
Maybe also update your python π
python 4 π
poetry?
It's a dependency management tool for python
I just want to deploy this app
I will circle back to that. For now, I just need this app running
Just saying, it's really great to use
Hmm okay so I'll learn django ORM :d I know basic SQL like creating tables and altering, inserting stuff
Im with you...and I really need this app deployed
"We need it yesterday"
Once I get this app deployed then I can learn more stuff π
Are you building something just to learn?
Progress....
[whurthy] [2022-04-23 23:23:54] => Build complete```
We're going to deployment!
Argh
Back to same issue
[2022-04-23 23:24:28] File "/usr/local/lib/python3.10/site-packages/gunicorn/arbiter.py", line 589, in spawn_worker
[2022-04-23 23:24:28] worker.init_process()
[2022-04-23 23:24:28] File "/usr/local/lib/python3.10/site-packages/gunicorn/workers/base.py", line 142, in init_process
[2022-04-23 23:24:28] self.run()
[2022-04-23 23:24:28] File "/usr/local/lib/python3.10/site-packages/gunicorn/workers/sync.py", line 125, in run
[2022-04-23 23:24:28] self.run_for_one(timeout)
[2022-04-23 23:24:28] File "/usr/local/lib/python3.10/site-packages/gunicorn/workers/sync.py", line 62, in run_for_one
[2022-04-23 23:24:28] self.notify()
[2022-04-23 23:24:28] File "/usr/local/lib/python3.10/site-packages/gunicorn/workers/base.py", line 75, in notify
[2022-04-23 23:24:28] self.tmp.notify()
[2022-04-23 23:24:28] File "/usr/local/lib/python3.10/site-packages/gunicorn/workers/workertmp.py", line 46, in notify
[2022-04-23 23:24:28] os.fchmod(self._tmp.fileno(), self.spinner)
[2022-04-23 23:24:28] PermissionError: [Errno 1] Operation not permitted
[2022-04-23 23:24:28] [2022-04-23 23:24:28 +0000] [4] [INFO] Worker exiting (pid: 4)```
This is literally the original error from the very first deployment, that took me down this rabbit hole
I really, really, REALLY do not enjoy deployment
Is there anything else in traceback?
Nah...that's it.
I'm going to try RUN mkdir -p $APP_HOME && mkdir -p build/static && mkdir -p build/tmp && chmod 777 build/tmp
Adding the chmod to the end after mkdir
Oh...good idea
RUN mkdir -p $APP_HOME && mkdir -p build/static && mkdir -p build/dev/shm
You don't have to call mkdir
yeah i'm building a portfolio website, also learning using django
its to apply for jobs mainly
Django would be good for that, FastAPI can render html templates too (if you're using django for that atm)
OK...going with:
RUN mkdir -p $APP_HOME && mkdir -p build/static
and
ENTRYPOINT ["gunicorn", "--worker-tmp-dir" ,"/dev/shm"]
Are you using backend rendering (templates) or are you also using a frontend framework like React?
only backend stuff for now, so i can get solid on it
I mean, you can use either of these frameworks
Both are capable
my understanding is very rudimentary and i'm following tutorials to build a django site lol
i guess thats what the render() function is in django? when I put in the 'base.html' template in
as an argument
i thikn its in the views.py
Yep, you're rendering html template
LOL
Now I have "Internal Server Error"
But that's different. Will shift to DEBUG True to check that out
Classic
yeah, so generally you're supposed to use a very lightweight init for the entrypoint and then the CMD is the actual program the entrypoint runs: https://github.com/krallin/tini
Maybe your migrations?
Thank you VERY much. Now I've got something to delve into
I'd already deployed to online PostgreSQL. So, connection is with live online DB
I don't think i really have use for that, at least at this moment
In short, I migrated to db in dev online to remove this as an issue
But did you run python manage.py migrate? 
Hmmm...I'll run in the console for this deploy just in case
In dev I'd run migrate and was working with the online db. Need to run migrate even though db is already built and running in dev app?
OH
OF COURSEq
I need to migrate the docker container~!!!!
oH My goodness
You should run your migrations when you deploy new version of your application, i use init container for that
version: "3.9"
services:
backend:
image: cool-image
networks: [internal, traefik-public]
backend-migrate:
image: cool-image
networks: [internal]
deploy:
restart_policy:
condition: on-failure
delay: 10s
max_attempts: 3
entrypoint: "poetry run python manage.py migrate"
Yes
It's a DB authentication issue...
AND, I'm actually OK with that. Finally got through docker deployment phew
ALRIGHT! So, now the issue is that css styles have not applied. BUT, the app is uP!
just curious, whats the advantage of deploying your django app through docker?
The biggest advantage is your installation is contained in a docker container. The big benefit is that it doesn't matter what platform you deploy to
You won't have to create specific environment for your application to run
e.g. installing python, dependencies, etc
Exactly. You build the image through docker, that builds the environment when you deploy
OH MY GOD! SO....I have site up. NOW, I just need to work out why css styles aren't applying. I'm betting I'm missing something related to the setting of the root templates directory!
In fact, not even the Django admin css styles are working...odd
So...css styles are not applying anywhere in this site...BUT, this is likely about STATIC_ROOT in settings.py. Need to turn off
hmmm so if I used docker to deploy my django app on a website, what steps does that skip?
Does anyone have much experience with azure cognitive search? I'm looking into it right now for implementing suggestions and autocomplete and I don't know much about it besides what's in the docs
setting up the virtualenv on the server(s)
also makes local development easier
you don't have ot install python or set up the virtualenv
OK. My AWS static files aren't being served up in this Django app...
So, I can't seem to get static files to display π¦
i see. so if i define python:3.10 in my dockerfile, then i can create containers it'll auto install python and whatever else i define ?
I've run collectstatic in the console and gotten the following:
I have 130 files but they're not showing on the site...hmmm
Django doesn't serve static files with debug=False
I know...which is why I have STATIC_ROOT root set and I am using AWS for static files
But even though the files are on the AWS object, they're not displaying
How does aws serve your static files from a container?
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3StaticStorage'
AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME')
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY')
AWS_DEFAULT_ACL = 'public-read'
# AWS_DEFAULT_ACL = None
AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'
AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=86400'}
AWS_LOCATION = os.environ.get('AWS_LOCATION')
AWS_S3_FILE_OVERWRITE = False
STATIC_ROOT = '/static/' # Added for deployment```
Good grief...I really do NOT enjoy deployment
I had originally tried this: https://docs.djangoproject.com/en/4.0/howto/static-files/
it's like anything else, just something you're not familiar with
So....according to django documentation...
I can:
- Declare STATIC_ROOT = '/static/' in settings.py
- Run python manage.py collectstatic
And yet.....that. dpes. not. work!
So, I try AWS...and that. does. not. work
Do you have your static files served on domain.com/static/?
Can you access any other files?
NONE
I am FOLLOWING the literal steps.
This is not ab out not being familiar. This is about following the steps and it not working
It is BS like this that drives me nuts
I have STATIC_ROOT = '/static/' and then run collectstatic...but nope
So, I know I have collectstatic in the docker image.
RUN python3 manage.py collectstatic -c --noinput \
&& chmod 777 $APP_HOME```
So, when the docker build runs it should collecstatic
I have STATIC_ROOT = '/static/' in settings.py
According to Django documentation that. should. work.
What am I missing?
No css files for admin styles...no css files for front end
TF?
I know collectstatic ran because when I run it again, I get told as much:
So what am I missing?
130 static files got moved to /static on build. Yet, nada front end. Why?
According the Django documentation I have followed the steps.
This is not about familiarity...this is about following the steps and missing something
What am I missing>
live docker container shows static folder
I can see files there...interesting, can't see admin files...
But I can see the favicon and css files
So that looks correct. collectstatic did indeed do that
So, if I can see a static folder in the container and I have STATIC_ROOT set, why aren't they showing on the front end or in admin?
Do I need to expose nginx?
I can see EXPOSE 8000 but I am not clear on whether I'm actually exposing nginx. No styling files are coming across at all
EXPOSE 8000 just means its open to the port 8000 right?
I believe so. I actually think this may be related to specific config for digital ocean...
I'm digging deeper. The good news is, once I work this out, it will be done. It's the final step for deployment
I've taken this to #help-rice
I'm definitely stumped here
you could use javascript for a delayed redirect like suggested, but you can also go with pure html: https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections#html_redirections
this will redirect the user after 3 seconds using only html:
<head>
<meta http-equiv="refresh" content="3;url=https://www.mozilla.org">
</head>
I don't know if this is the right place to ask but in django is it faster/cheaper on processor and memory to use a "for loop" to count/sum db entries or to use query aggregators?
Eg.
X = model.filter
S =0
For I in x: s+=l.amount
Vs
X =model.filter().aggregate(sum())
I'm absolutely stumped. Deployed django app in docker-container to Digital Ocean. Static files won't be served either through nginx or AWS. I've been sitting for sometime in #help-rice
IF anyone has experience with deploying Django in Digitial Ocean through docker I would really appreciate you taking a peek.
Hey Iβve seen you around for a bit. Iβve never deployed but I can help you just look through you settings.py and see if maybe Iβll catch something youβre not seeing.
Thanx. I"m literally several hours on this.
settings.py py STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static')
thanks!!
Remember, I'm deploying to production...so I need to declare STATIC_ROOT
But even though the files for admin have been collected into the static folder, admin renders like this....
That is a broken admin django page. The admin styling files simply aren't being served
When I inspect Network I see...
NONE of this makes any sense
import io
import os
import dns
import uuid, base64
from flask import request
from flask import Flask
from flask_sock import Sock
app = Flask(__name__)
sock = Sock(app)
@app.route('/')
def index():
return "<html><body style='color:white;background-color:black;'>An dumb api</body></html>"
@app.route('/LOLZ')
def Upload():
return "LOLZ"
@sock.route('/echo')
def echo(sock):
while True:
data = sock.receive()
sock.send(data)
app.run(host="0.0.0.0", port=443)
``` y dis not work
This may be a no brainer but have you checked your url paths.
I've not needed to add url paths for serving static files before (Linode). There is no mention of needing to do this in Django docs
What I am noticing is that the IDE is showing errors on the calls to those css files....now that I've switched to production settings
Oh I see
OK...I have found a problem I can't workaround...
To move to production I can't have STATIC_ROOT and STATICFILES_DIR py STATIC_ROOT = os.path.join(BASE_DIR, 'static') STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ]
HOWEVER, removing STATICFILE_DIRS breaks the include static in the templates
Iβm doing some reading and Iβm seeing that you have to find a way to set the x-content-type-options in your deployment env.
@ionic raft check this https://www.digitalocean.com/community/questions/how-do-i-add-custom-http-headers-to-a-hosted-static-site
The answer on that thread isn't encouraging
The build log shows collectstatic deleting the css files!
[2022-04-24 02:54:02] INFO[0029] Running: [/bin/sh -c python3 manage.py collectstatic -c --noinput && chmod 777 $APP_HOME]
[2022-04-24 02:54:03] Deleting 'Whurthy/events-css.css'
[2022-04-24 02:54:03] Deleting 'Whurthy/favicon.ico'
[2022-04-24 02:54:03] Deleting 'Whurthy/main.css'```
Why is that happening???
I just read that the path provided in static_root can't be in staticfiles_dirs
Indeed. I had to comment that out
Which I did. The end result is static files simply aren't being served by this hosting solution
This is so sad....I've got the site deployed...but simply won't serve the static files.
Thanks for tyaking a look
I've been banging my head against this all day. I need to step back.
This is a crazy situation
Well, it's going a little less smoothly than I had hoped.
However, I did get the site deployed today. So that's something
Hmmm....may have had some progress...the files are not being deleted now that I've namespaced them in a static folder within an app rather than the root directory
I suspect nginx config is the problem
This is an nginx issue. I need to configure nginx to correctly serve up static files
Oh alright thats a lead
It is. Now I just have to work out how to configure the default.conf file to correctly serve up static files on port 80
Anyone with flask and SQL experience can help me debug, I'm building a movie booking system and it is 90% done but am having minor issue and I cant seem to find what's wrong #βhelp-coffee
(django) I think I have found a bug in drf. I have custom permission which when returns false the response should have
{
"detail": "You do not have permission to perform this action."
}
which does happen but if the user is not authenticated the response has
{
"detail": "Authentication credentials were not provided."
}
even if the reason for not permitting wasn't authentication
Anyone with flask and SQL experience can help me debug, I'm building a movie booking system and it is 90% done but am having minor issue and I cant seem to find what's wrong #help-lemon
hi, im create appointment, i am stuck at to create functions max limit per time . How to set max booking per time If I say I want to set every time an example at 8AM users can book max limit 10 times
from django import forms
from booking.models import BookingSettings
class BookingDateForm(ChangeInputsStyle):
date = forms.DateField(required=True)
class BookingTimeForm(ChangeInputsStyle):
time = forms.TimeField(widget=forms.HiddenInput())
class BookingCustomerForm(ChangeInputsStyle):
user_name = forms.CharField(max_length=250)
user_email = forms.EmailField()
user_mobile = forms.CharField(required=False, max_length=10)
class BookingSettingsForm(ChangeInputsStyle, forms.ModelForm):
start_time = forms.TimeField(widget=forms.TimeInput(format='%H:%M'))
end_time = forms.TimeField(widget=forms.TimeInput(format='%H:%M'))
def clean(self):
if "end_time" in self.cleaned_data and "start_time" in self.cleaned_data:
if self.cleaned_data["end_time"] <= self.cleaned_data["start_time"]:
raise forms.ValidationError(
"The end time must be later than start time."
)
return self.cleaned_data
class Meta:
model = BookingSettings
fields = "__all__"
exclude = [
# TODO: Add this fields to admin panel and fix the functions
"max_booking_per_time",
"max_booking_per_day",
]
If you deploy a web app on a VPS like linode, how do you edit the code of the web app and deploy the new version? Which steps do you have to repeat?
that looks about right to me, one first checks if the user is logged in (authentication) find out the identity of the user
after you got the identity of the user you can check what that user is permitted to do (authorization)
so, you need authentication checks to be done before you can even go on to do authorization checks
You replace old code with new code π
Unless you run your app in container
do you need to reload gunicorn?
Yep
do you need to reload nginx?
no
if you close the putty/ssh would the server stop working?
if you have started nignx and gunicorn correctly (as daemons) that should not happen/be a problem
ok
I am getting this error when using supervisor with gunicorn in deployment with linux ubuntu server
supervisor: couldn't chdir to /home/flask_app: ENOENT
supervisor: child process was not spawned
First observation - use ticks to format code...three of them for multiple lines...a single tick each side for a single line:
print('Look how readable this code is!')
Second observation - if you want to control input going with forms.py for the app, importing the model, and building a form with def clean(self): method you can do great validation.
You can also validate in view.py, but I find forms more logical, intuitive and understandable. You could do your validation upon form submission.
Third observation - you could also add logic to views.py to calculate the number of bookings (per your requirement) and adjust the template from there.
!format
String Formatting Mini-Language
The String Formatting Language in Python is a powerful way to tailor the display of strings and other data structures. This string formatting mini language works for f-strings and .format().
Take a look at some of these examples!
>>> my_num = 2134234523
>>> print(f"{my_num:,}")
2,134,234,523
>>> my_smaller_num = -30.0532234
>>> print(f"{my_smaller_num:=09.2f}")
-00030.05
>>> my_str = "Center me!"
>>> print(f"{my_str:-^20}")
-----Center me!-----
>>> repr_str = "Spam \t Ham"
>>> print(f"{repr_str!r}")
'Spam \t Ham'
Full Specification & Resources
String Formatting Mini Language Specification
pyformat.info
Massive thanks again, bruh! Site deployment is such a relief, and also passing all testing so far too!
glad to hear that things are smother for you now π
"Dr Carewen or: How I Learned to Stop Worrying and Love Deployment" movie reference with a twist π
Nah...I'll be hiring expertise for deployment. π
But it was a growth experience
does anyone know any suggestions to help with this error?
Unfortunately not. That looks like a perms issue in Linux though? Actually...on second thoughts...it's changing dir...so are there read perms on that dir?
I am new to Linux. Is there a way to check this?
I'm not that experienced either...but ah yes...ls -la π
try ls -la /home/flask_app
can you just do this instead: ls -ls /home
that's light on perms...some read but not comprehensive?
your error message said that supervisor was trying to chdir (or cd, change directory) into /home/flash_app
is there a supervisor configuration file that needs to be changed to have the right path maybe?
that's a good question. Not having perms would throw an error from glancing
is -3 high enough perm level?
I am newish to Linux too...so pls don't listen to me π
this is my error message
When is supervisor being given perms?
https://www.linode.com/docs/guides/flask-and-gunicorn-on-ubuntu/
i am following this guide
This guide walks you through the steps to deploy a Flask application to a production environment running on a Linode.
okay, at least the path looks better now
Flask...good times π
Funny how I spent a month with flask and then went to Django...because amazing π But Flask was solid
so, next step is FastAPI then π
I've been doing the Django-Tango with ORM...building apps with PostgreSQL. Not had much use for API work, except to do things like send texts and emails.
I LOVE Django's ORM. I'm really low on SQL skill ladder...but what I can do with ORM in Django...good times
The app we deployed is at https://whurthy.com by the way. In alpha testing, so not much write up about it...
Let's just say that Whurthy does things that we've not seen any other event mgmt solution do
Which reminds me...I should really write something up for non-authenticated users on the home page. But it's actually a B2B solution.
FastAPI is not just about APIs even if the name would suggest so
So if I were opening up an API, FastAPI looks to be a solid option?
That'll be needed for when we want to build some apps
probably not if your app is already written for django, then it's easier so stay in django-land
But now...the Django-Tango is the dance I'm in
Ahh...
I really do enjoy building apps in Django. Whurthy took me 93 days from idea to deploy
AND, it looks like with Digital Ocean I've the basis of rapid deployment. Although, I really do need to get that refined. Good for testing for now
The ORM in Django, really lots of fun to use. Very intuitive
we just have to agree to disagree, but whatever floats your boat π
I will confess though...PyCharm....Use PC and the ORM is a tame kitten.
Go blind for querysets...and that's arse
So I wouldn't do ORM without an IDE
Not interested in wasting my time
Love me some search on the fly, not gonna lie π
And contextual search...none of that unguided cross site searching either π
you are deploying the static resources to aws s3, which is fine unless you want to avoid having aws as well for cost reasons or similar
if you would have wanted to serve the static files from your container where you run your django app you could have been running both gunicorn and nginx in the same container using systemd or supervisor or anything like that and change your entrypoint in your dockerfile accordingly, even if it's not best practice it can save money/cost of not having to use multiple containers or service providers
Oh that's not a perm solution. It's for alpha/best testing. I'm planning on hiring expertise to architect deployment and CI/CD pipeline
This is not a task I want to learn and apply for WHurthy. Want deep expertise, someone who knows what they're doing.
It's a B2B solution, where each business (client) will have their own DB/instance
that's going to mean a big need for CI/CD pipe, and some elegant architecture both from a scalability and operational POV
This app looks so sweet on my smartphone, I'm not so sure iOS/Android apps will be urgent
doesn't sound like cost is the primary concern for you then
then you should probably stick with one daemon per container
And no deep JS....so silly fast
NAh. It'll be subscription.
The standout part for me...using htmx to render inline forms that are so easy to use. Really enjoying the UX on my phone for managing bookings
Django's built in inlineformfactory...gets the job done, but it's nasty
Htmx for inlineforms...the holy grail of repeating data input
Someone experienced with selenium? i need help with a thing
it's better if you put text in the chat as text instead of a screenshot
!code
Here's how to format Python code on Discord:
```py
print('Hello world!')
```
These are backticks, not quotes. Check this out if you can't find the backtick key.
hello everyone
in need help wuth some html
i need some help in django webframwork
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>BuzzMoney</title>
<style>
.btn {
height: 100px;
width: 100px;
position: relative;
top: 350px;
left: 710px;
padding: 0;
border: none;
background: none;
}
.Balance_text {
font-size: 150%;
}
</style>
</head>
<body>
<button class="btn" onclick="button_pressed()">
<img
class="Image_of_btn"
src="EARN MONEY.png"
alt="EARN MONEY"
height="100px"
width="100px"
/>
</button>
<p class="Balance_Text" id="Balance"></p>
<script type="text/javascript" src="index.js"></script>
</body>
</html>
i am changing the font size
but still the font size isn't changing in the website
the class name must match and is case sensitive
in your style definition you have Balance_text (lowercase t after the underscore) and the class set on the div is Balance_Text (uppercase T after the underscore)
In this community you will quickly discover the power of a good question. "need some help in Django web framework" could benefit from more information. What are you aiming for? What is happened? What have you tried?
First, you'll want to shfit from <style> to css files ASAP. Bootstrap5 or Tailwinds are wonderful tools.
Second, I find REM more reliable for sizing.
but isn't html and css not case sensitive
A good question. Only one way to know...prove it either way. Coding is about trying lots of different angles when you get stuck.
And getting stuck is where you really learn
i know
not when it comes to class names and the id labels, they require an exact match
now i get it man thanks
Which can be useful π
now i am not able to move the text
in .Balance_Text {
position: relative;
top: 100px;
}
it aint moving
So I finally sat down and wrote out what Whurthy does after 93 days of Django....
as i'm more or less a full time linux user (yes, on desktop as well) i'm use too things like the file system being case sensitive like that
And I just remembered you can track seating by venues too.
Django is an AMAZING web dev framework....truly AMAZING
nice, but i would drop the "here" part for links and just make the verbs/actions and such the links
like "login here" -> "login" and "register here" -> "register" and "reset it here" -> "reset it"
Good call π
Already deploying those changes. Love me some automated pipeline from github to auto deploy
And done π
hmm is docker only effective in deploying web apps?
you got your deployment flow down now? π
not desktop apps, because you can just package them and make an installer?
Deployment is setup.
AND, it will need to be enhanced over the coming months. It's OK for a single-dev. Need a gitlab type solution, maybe some Jeeves
not many people other then developers will run docker on their own computers
but docker is good for a lot of other stuff then just web apps, just not great for desktop apps for the average user
on linux you have something in the vain of docker but for the desktop apps and other application packages in things like flatpak, snap[craft] and appimage
but on windows and mac the os is already much more uniform for each of them, so the need of such solutions is not as big of an issue
hello is there a way to call a JS function into a Django form ?
what do you mean by this?
Can you explain more? I use JS to hide and reveal fields in forms.
Couple of observations. First, use formatting (link to follow) for code. Second, can you use those lovely backticks to post your settings.py for DB? EDIT: Oh, and I suspect models.py from the...users app (?)...may be in order.
!format
String Formatting Mini-Language
The String Formatting Language in Python is a powerful way to tailor the display of strings and other data structures. This string formatting mini language works for f-strings and .format().
Take a look at some of these examples!
>>> my_num = 2134234523
>>> print(f"{my_num:,}")
2,134,234,523
>>> my_smaller_num = -30.0532234
>>> print(f"{my_smaller_num:=09.2f}")
-00030.05
>>> my_str = "Center me!"
>>> print(f"{my_str:-^20}")
-----Center me!-----
>>> repr_str = "Spam \t Ham"
>>> print(f"{repr_str!r}")
'Spam \t Ham'
Full Specification & Resources
String Formatting Mini Language Specification
pyformat.info
By that I mean exactly what @ionic raft pointed
It's exactly what am trying to achieve but without success, can you help this week plz ?
If I have a HTML <a> element that goes to mywebsite.com/hello and have a Flask route for "/hello" Flask will still pick it up right?
I'm trying to clone a repo but I am getting an error
[14:55]
can I get help?
[14:55]
jsoto-codes@jsoto:~/repos$ sudo git clone git@github.com:js-machinecode/github_test.git
Cloning into 'github_test'...
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
[14:56]
https://0paste.com/368642#hl
hi guys, i remember there was a tag/extension that can make the form more cute
hi guys, i remember there was a tag/extension that can make the forms look more cute
can you tell me which one it was ?
my code look ugly
ping me if you can help me
@native tide download crispy form then {{form |crispy}}
yes, when the link is clicked/activated
yeahh it was that, thanks bro
Welcome feel free to ask me anything
Do you know how can i automate the process of creating a profile (i made a model called profile attached to the User with OneToOneField) when a User is created ?
@native tide you can use the usercreationform provided by Django to do so !
def register(request):
if request.method == 'POST':
form = UsercreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
messages.success(request, account has been created! ')
return redirect('your template name to login')
else:
form = UserCreationForm()
return render(request, 'you template to registerl', {'form': form})```
Hope it helps you
Yeah i already made this
www.paste.org - allows users to paste snippets of text, usually samples of source code, for public viewing. A place where lack of code gets binned; sharing code iterations since 2006.
check this @fickle basin
in flask is there a way to have it not log favicon requests e.g.
2022-04-24 17:34:30,708 - [werkzeug | _internal.py:224] - INFO - 127.0.0.1 - - [24/Apr/2022 17:34:30] "GET /favicon.ico HTTP/1.1" 200 -
for every request
There nothing wrong with the code I presume,.does throw in error?
Throw you should use a Foreignkey rather than a OneToOne relationship
Letβs say Iβm making a blog and a user searches for a blog title. So the server returns any blog whoβs title resembles what the user searched. What I want are the blog titles to be displayed and the client can then click the blog titles to see the full blog on a new HTML page where the link is something like myblog.com/blogs/blogtitle is there any efficient way to do this? I was thinking Everytime a new blog is added a new HTML file is created but im not sure about how efficient that is.
The link has to be myblog.com/blogs/blogtitle so that users can copy and paste it
i'm making a script with selenium but i couldn't find some element in a page, so i printed the html code of it and i get this:
but actually the html code of it is this:
does that mean that the page blocks bots?
also good for SEO to have the url like that and not an id or such π
I guess you can just display them in a list and use a primarykey with href
what happens if you instead follow that link for the embedded iframe?
wdym? i'm new with selenium
i the html you see a <iframe html element with a src attribute which is a url, what do you get if you follow that url?
it's the same link to the page i'm connected
Hey, I'm looking for good fastapi project with implemented "everything" correctly
for exemplary structure of code etc. (with tests, database interactions, pydantic schemas etc.)
Does anyone by any chance have it? π
struggling with this problem for last few days... I have set up a MySql 8 db with django 4 and python 3.10; I want user to see their updated balance each time a new transaction is added via web form. Should you calculate their new balance at the db level, or within the django form, or in a separate python function?
i'd say db
ok still trying to get my head round how to do this. i've been following the Learning SQL and SQL Cookbooks and files can't see a way to run a direct calculation between two or more fields
what kinda calculation are we talking here?
quite basic... just a current balance column (add or subtract newest value from previous total, say if they are spending or adding to their account)
are we talking about the same fields here?
I am a big fan of crispy forms π
There are two main schools of thought. The bottom heavy school recommends striving for coding logic at the model level. Tracking a balance against a record makes sense.
I'm only a year into Django, and the DB I've been using has been PostgreSQL (so not much help on that regard). The app heavy school tends to build logic into views.py. That is infinitely better than putting logic in your template π
forms.py is great for declaring different flavours of form, and I make great use of validating input at that level.
Speaking truthfully, I plan on learning more about how to work more bottom heavy, but I'd likely approach this by putting logic in views.py and use ORM to write to DB. I could envision some methods at the model level for doing things like returning a balance (but that's a pure guess).
Good luck, bruh
i'm using SQLite (library not cmd line), how do I delete a record based off of its name and not ID?
its a bill tracker app, and there are 2 columns.. bill_name and cost
Querying with Django?
no its on tkinter
Hmmm...web dev might not be the ideal channel for tkinter. I did dabble with back in a boot camp. Had fun. Foggy memz though.
ok ty
man i just found out about django tailwinds, man im in love
regardless if it's web or tkinter you might have some luck in #databases
otherwise, going without an ORM here (warning: untested code, just writing it from memory):
with conn:
sql = 'DELETE FROM bills WHERE bill_name=?'
cur = conn.cursor()
cur.execute(sql, (bill_name,))
conn.commit()
css frontend frameworks are a wonder, for both style and presentation. Enjoy
thanks for replying. I'll try it out, i'm starting to think that i need to instantiate the cursor and commit for every sql query I use
hello , i know basic python programming ..whenever i see utube for software engineer , i see them using java or something else shall i switch to java too? or is python also good for getting a job
you should never follow what others are learning , learn a language which fits best for your work or projects imo
anyone knows how i can make a second page for my website in html
for example
if my website url is example.com
then i want my second page to be example.com/example1
so how do i do that
if it's for job opportunities your learning you should check what languages ar needed/requested in your geographical area (unless you are going to work remotely) and that fetches a salary that you would be happy with... and most important of all, you should like what you do to get really good at it and not see every day as just pain and/or boredom
what do you use for hosting the web pages?
Hi, I am trying to create a field with RadioField but it only allows 1 button selected. I want multiple selection, how do I do that?
you can't do that with "radio" if they are part of the same group (having the same name attribute):
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/radio
what you can use instead is "checkbox" or "select multiple" (like a list):
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/select
It's been a while since I've done scss. Do I need the & when doing & a {}
Currently, I'm using session variables to pass some information between my flask app. Should I change this to something else that might be more secure? Or is this okay?
<button>
<div id="box">
</div>
<i class="fa-solid fa-house-chimney-window" style="font-size:9.2em;"></i>
<h1 style="margin-top: -18px; font-size: 0.6em; color: gray;">Tobias LV</h1>
</button>
How would I go about making the window only yellow?
That's a serious question π
You could use a link instead of a button and position a dive behind it and make the div yellow
Or use an svg I guessπ€·πΎββοΈ
for the svg
i think you have some problem with pip and how it's installed
Anyone know any Odata Libs for python, seems like everything I am finding hasn't been updated in ages.
the internet says: python3 -m pip install --user --force-reinstall -U and that --user for the reinstall and upgrade is the important bit
can someone help me with flask
I am trying to use some static files but i am code code 500 and some weird traceback
here is the code
why is the traceback weird
Hi, A got a question on flask.. IΒ am using Flask appbuilder.. how is it possible to return to the same page, upon action;Β https://flask-appbuilder.readthedocs.io/en/latest/actions.html
thanks this is a great reply! good to know i'm not going crazy by considering there are various approaches that might be suitable and schools of thought. I think 1 day of focus and can smash this (hopefully π !)
If you smash this in a day then good for you! I'm still getting my head around working with views for functionality. π€ͺ
my icons are blurry but when you hover over them for a second they are fine, why?
Unsure where else to put this -- I'm looking for some help with the YouTube API.
I want to create a playlist on a given channel, but I'm unsure how to do that. I've got an API key for the account the channel is associated with, but https://developers.google.com/youtube/v3/docs/playlists/insert#usage doesn't seem to explain to me how I can choose which channel associated with my account I can use.
Many thanks all.
hey mates. I started learning Flask + Python a couple of weeks ago and I built an app with database, you can register, login and obviously logout. you have to register to see the content on the web app. I was wondering what could I make out of this. I was thinking about building a ToDo list in there but not sure...
I want to build a flask project for an internship
Rate this: 
I rate it if you help me haha
I'm trying to scrape edabit.com's python challenges to make a script for automating my process, but none of the actual content is in the source code for the website. Any workaround for this? Here's an example challenge https://edabit.com/challenge/JzBLDzrcGCzDjkk5n
Edit for clarity: I want to scrape the description, notes and examples etc
@brave tulip did you add the html to the page like this https://www.w3schools.com/tags/tag_doctype.asp ?
is beautiful soup considered a automated testing tool like selenium?
yes
but i fixed it by now
Hmm, from the looks of it the actual content is fetched and rendered with JS after the initial HTML is served, and using Python to fetch the page isn't going to run that JS code.
i wouldn't call it that at least
I think bs4 is best for parsing out the <meta> tags of html
i.e. when you post a link in discord, the discord backend calls that url and finds meta tags to populate the embed
beauitful soup parses html, so its good for web scraping (gathering data from websites). if you want to sift through html to grab certain data from certain websites, you can do that
i feel like you can make a script that can auto-buy items when they're in stock
with selenium + bs4
hey guys, im making a website application thing that features a sorting visualiser
the backend is gonna be coded in python - the coded algorithms and related helper code
and the visualisation is gonna be in js
im wondering how i can transfer data across the languages efficiently
using json?
will that be quick?
What backend framework are you looking to use?
probably django or flask
with flask, you can return jsonify(data) to return a json response
yeah but how will i make thatt connection so the two languages can interact with each other
will it be like a server or database?
from javascript, you would make a rest request using fetch() to get the data.
and python would do a POST?
def get_variables():
return inspect.currentframe().f_back.f_locals
def is_deepest_loop(node):
return not any(
child != node and isinstance(child, (ast.While, ast.For))
for child in ast.walk(node)
)
class FunctionTransformer(ast.NodeTransformer):
def visit_For(self, node):
self.generic_visit(node)
if is_deepest_loop(node):
return ast.For(
target=node.target,
iter=node.iter,
# body=[ast.Expr(value=ast.Yield()), *node.body],
body=[ast.Expr(value=ast.parse("yield get_variables()")), *node.body],
orelse=node.orelse
)
return node
def visit_While(self, node):
self.generic_visit(node)
if is_deepest_loop(node):
return ast.While(
test=node.test,
body=[ast.Expr(value=ast.parse("yield get_variables()")), *node.body],
orelse=node.orelse
)
return node
def bbsort(array: list):
index_length = len(array) - 1
swap_counter = -1
while swap_counter != 0:
swap_counter = 0
for i in range(index_length):
if array[i] > array[i+1]:
array[i], array[i+1] = array[i+1], array[i]
swap_counter += 1
index_length -= 1
because right now, i have made itt possible for you to input your own sorting algorihtm
and it would get converted into a generator that returns the "debug" data every time u do next(bbsortInstance)
and that debug data would get sent to the JS
quick example. ```py
app = Flask(name)
@app.get("/getData")
def get_data():
return jsonify(hello="world")
```html
<html>
<body>
<p id="data"></p>
<script>
fetch("/getData")
.then(data => data.json())
.then(data => {
document.getElementById("data").innerText = JSON.stringify(data);
});
</script>
</body>
</html>
is there any documentation/websites i can read up on this because i don't want to keep pestering you
I suggest the mdn (mozilla developer network). It has great community documentation.
thanks!
Does anyone have suggestions on how to implement full text search in flask?
i am websraping python gpu information and i am h aving trouble getting the user rating
this is the line i want to pull
<i class="rating rating-4-5" aria-label="rated 4.6 out of 5"></i>
and this is how i am trying to pull it
ratingElementFourStar = containerElement.find("i", class_="rating.rating-4-5")
but it outputs
None
any ideas?
this is my whole file:
#https://realpython.com/beautiful-soup-web-scraper-python/ < tutorial
from bs4 import BeautifulSoup
import requests
page = requests.get("https://www.newegg.com/p/pl?d=rtx&N=4131%204841%204814%204021%204020%204019%204018%204017")
soup = BeautifulSoup(page.content, "html.parser")
results = soup.find(class_="list-wrap")
container = results.find_all("div", class_="item-container")
for containerElement in container:
priceElement = containerElement.find("li", class_="price-current")
nameElement = containerElement.find("a", class_="item-title")
ratingElementFiveStar = containerElement.find("i", class_="rating.rating-5")
ratingElementFourStar = containerElement.find("i", class_="rating.rating-4-5")
print(priceElement)
print(nameElement)
print(ratingElementFiveStar)
print(ratingElementFourStar)
print()
neither of the ratings work, they both print python None
@open sierra on mobile so I haven't checked, but I'm guessing the data is not sent with the HTML. it's probably loaded from some JSON via fetch/XHR
The other things work so how am I supposed to pull the rating?
assuming I'm right, request the same JSON
How would I go about that
I do not know how to request JSON
And how did you know itβs JSON?
I'm just guessing. use your browser's devtools' network tab to check
If you want to search for multiple classes on a single element, I don't know that using the class_ keyword argument like that will work
you may have to do something like
containerElement.select(".rating.rating-4-5")
But wonβt this only find one instance of this element instead of all times this element appears
Or class
Iβm new to html and python and web scraping
this is more library specific knowledge than python. I'd have to check the docs or just try it
it looks like it returns a list
confused about?
this works for me
import requests
from bs4 import BeautifulSoup
response = requests.get('https://www.newegg.com/p/pl?d=rtx&N=4131%204841%204814%204021%204020%204019%204018%204017')
soup = BeautifulSoup(response.content, features='lxml')
four_of_five = soup.select('.rating.rating-4-5')
print(four_of_five)
[<i class="rating rating-4-5"></i>, <i aria-label="rated 4.6 out of 5" class="rating rating-4-5"></i>, <i aria-label="rated 4.7 out of 5" class="rating rating-4-5"></i>, <i aria-label="rated 4.7 out of 5" class="rating rating-4-5"></i>, <i aria-label="rated 4.6 out of 5" class="rating rating-4-5"></i>, <i aria-label="rated 4.7 out of 5" class="rating rating-4-5"></i>, <i aria-label="rated 4.6 out of 5" class="rating rating-4-5"></i>, <i aria-label="rated 4.6 out of 5" class="rating rating-4-5"></i>, <i aria-label="rated 4.4 out of 5" class="rating rating-4-5"></i>, <i aria-label="rated 4.6 out of 5" class="rating rating-4-5"></i>]
you should really avoid eval() and only use it as a very last resource (many times one can redesign ones solution to not need it) and be ware of possible security risks associated with using it
ah yes im aware of it, but my school needs me to exploit it on a website
but @nimble berry there are restrictions in it
@outer apex right, i see, thanks.
If an internship expects me to build a flask project, what would you recommend?
I finished Miguel's book
Know the foundations
Also have experience with html, css, JavaScript
But just started learning Python a couple weeks ago
have they explicitly instructed you to use eval()?
they really should discourage the use of that unless it is explicitly to demonstrate the very dangerous nature of the use of eval() in your code
even if one thinks one has put restrictions and safeguards in place, it is very easy to miss edge cases where someone will be able to exploit it for nefarious purposes
we were instructed to exploit it
the best way to get help with anything on here is to just ask the question
its a capture the flag
aha, i see
we are given a website and we are told that a website uses eval in its python code
than the exception i stated above applies π
it is to explicitly teach you the dangers of using eval() and why you should avoid it at almost any cost
#cybersecurity might be even better for this question then
ok thank you rndpkt
so the issue is when i use the p function and make text in html it creates spaces on the right of the website
how do i make so that it doesn't create any space
hi can any one help i am having error in django
yaa
ok
thanks its get working now
any way to check what the user is currently typing in to a html form (before enter/ submission)
CSS subclass .active or .focus is active as far as I remember, while he has fields in focus/cursor there
although describe what u wish to achieve, perhaps u ask the wrong question and receive answers for a wrong question.
i want it to autocheck the input after each letter and make it submit if a condition is true
i'm currently using flask
Does this select every instance of this class?
How to use value with space? Like computer.computer name
what do you mean by with space
oh, it's python, you gotta use underscore
show your python code
Ok, just a moment
ah
I have to change "computer name" to underscore instead of space?
the your variables are ```html
...
<td>{{ computer["User"] }} </td>
<td>{{ computer["Computer name"] }} </td>
<td>{{ computer["Computer Brand"] }} </td>
...
Ok
No .?
yes
it's a dictionary {"key":"value"}
yes because there is no dot and your "user" was capitalized -> "User"
Ok
how u can access key data: @formal rapids
myDict = {"key":"value"}
print(myDict["key"]) #OUTPUT: value
can you share it again, I'm wondering why there is a for loop
@inland oak
There is a for loop because im accessing rows in database
So it'll get value for each row
oh okay
nevermind then
If there was no space i could access it like computer.User
Without the brackets but with spaces it failed
Spaces i meant
use the brackets
https://stackoverflow.com/questions/574941/best-way-to-track-onchange-as-you-type-in-input-type-text
https://www.w3schools.com/jsref/event_onchange.asp
it looks like javascript onchange event then
people do it in elegant way in Javascript Frontend Frameworks though
they have mvvm data binding model things which makes stuff like that easy without any hacks
Why did you use select?
Hello
so in css the background-color is the color of both content and padding boxes right?
I think so
for easy , quick websites that are secure, would django or flask be the better option?
keeping mainly security in mind.
is anyone else having problems with visual studio code
hmm, do you know if flask has something like that
I'll probably migrate to js
no, it does not. Flask is not having frontend JS for that.
Flask can only have those triggers when the user presses submit, then it reacts with msgs
you need to use Vanilla JS, JQuery or linking statically Vue.js for example, or just making frontend in the frontend framework: React / Vue.JS / Angular
building frontend in frontend frameworks is the best option. All the previous options are substitutions, than worse than closer to the beginning
mr sirs, i am facing an error in django when trying to go GET requests
i will now post error log in text file
Hey @high hill!
You either uploaded a .txt file or entered a message that was too long. Please use our paste bin instead.
oh wow
wow thanks for posting the FULL traceback
Looks like your connection timed out
sorry im dumb idk how i should post errors T_T
yea so what is happening is, this error comes around randomly
is it polling or alerts or smth
No no, we're thanking you
im doing GET requests to a separate deployment, lets say every 10 secs (total number of GET requests are 15) and randomly this will happen
oh XD, thanks
People never post full tracebacks and it's shit hard to know what actually went wrong
oh also, i thought this was because of djangos inbuild WSGI so i used gunicorn, still same error ;-;
Looks like the separate deployment you're trying to ping times out/doesn't respond
hmm, so this shouldnt be an of django right ???
does it happen if you increase the period between requests
Yeah, give the server some more rest
Try to ping it every 30 seconds
Just for debugging
hmm, I am testing it right now, in the end i will keep the interval at 5-10 mins
icic, ill try 30
though you know, sometimes it happens even when it has run 0,1 or 2 times too
That wouldn't be very consistent, perhaps they could catch errors and every error increment it and have a system when the errors get lower they speed it up
that'd be cool, like an "exponential backoff"
so it goes something like
20 | 20 | 20 | ERR | 30 | 30 | 30 | 30 | 20 | 20 | 20 | ERR
no issues, forget about it
yeah! of course i can't comment much because i don't know what their seprate server is doing but they'll need to find a pattern that works for them
yea i dont know of specifics either
i did this for a website i made a lil while ago
works well if you know the pattern
hmm, i guess i could do this if it persists when im testing it on cloud
try it out
but right now on local when i do this, the port-forward just closes, so i have to do that again manually
aah
yaah D:
do you controll the external server?
that must be annoying asf
super
maybe startoff by making a resstart system
if that's an important endpoint it'll be a pain to have it go down randomly
π
just something that keeps pinging the api every 10 minutes or so
with their VPS sure iirc
hm
sure
he vanished
just ask the question
Alright
I built an app with database, with the help of Miguel's book, you can register, login and obviously logout. you have to register to see the content on the web app. I was wondering what could I make out of this. I was thinking about building a ToDo list in there but not sure...maybe doing something with API? I want to make a web app / page to have a project for an internship.
Do you have any idea?
make a up/ down vote website
you can do either server side rendering (which is an old approach but brought back to life because some react frameworks) or build an API and consume it from frontend. Which one is the best is up to you and depends on context. Usually you want to build an API when you are building something that needs to be consumed by different clients (mobile apps, web interfaces, desktop application, other APIs, etc)
Hmm
I have experience and projects with JavaScript and html and css, I switched to python and flask because an internship wants me to. I like it so far. Do you guys think its bad if I use help from stackoverflow or books?
Thanks for the help tho!
Appreciate it
any help is good even more if it comes for free π
π 
yes
because the docs suggested using select if you are trying to find elements using multiple classes
also, I'm familiar with css selectors so using this method is easier
Hmm
Ah okay thanks that really helps a lot
What doc are you talking about btw
no problem
The beautiful soup docs. Specifically this https://www.crummy.com/software/BeautifulSoup/bs4/doc/#searching-by-css-class. They talk about using select in this section.
Oh cool thanks
Is there even a thing called as package in python?
My heroku just dont wanna read it
what
@indigo kettle it still doesn't work. it outputs ```python
[]
this is my .py file:
```python
#https://realpython.com/beautiful-soup-web-scraper-python/ < tutorial
from bs4 import BeautifulSoup
import requests
page = requests.get("https://www.newegg.com/p/pl?d=rtx&N=4131%204841%204814%204021%204020%204019%204018%204017")
soup = BeautifulSoup(page.content, "html.parser")
results = soup.find(class_="list-wrap")
container = results.find_all("div", class_="item-container")
for containerElement in container:
priceElement = containerElement.find("li", class_="price-current")
nameElement = containerElement.find("a", class_="item-title")
ratingElementFiveStar = containerElement.select(".rating.rating-5")
ratingElementFourStar = containerElement.select(".rating.rating-4-5")
print(priceElement)
print(nameElement)
print("rating is next")
print(ratingElementFiveStar)
print(ratingElementFourStar)
print()
when i run this it outputs:
<li class="price-current"><span class="price-current-label"></span>$<strong>498</strong><sup>.98</sup> <span class="price-current-range"><abbr title="to">β</abbr></span></li>
<a class="item-title" href="https://www.newegg.com/Product/ComboDealDetails?ItemList=Combo.4491308" title="View Details">ASUS Dual GeForce RTX 3050
8GB GDDR6 PCI Express 4.0 Video Card DUAL-RTX3050-O8G and ASUS TUF GAMING B450M-PRO S AM4 AMD B450 SATA 6Gb/s USB 3.0 HDMI Micro ATX AMD Motherboard</a>
rating is next
[]
[]
it outputs more than just this but for each item it looks like this
anyone else have an idea? im trying to grab an item rating from newegg using this line of html code: ```html
<i class="rating rating-4-5" aria-label="rated 4.6 out of 5"></i>
In Django, say I had two models:
class a(models.Model):
x = models.BooleanField()
class b(models.Model):
y = models.BooleanField()
Is there a way to only let y be True if x is also True?
hmm, it looks like maybe I need to look into making a custom save method
No, an object of class a would exist first. I'm trying to prevent an object of class b's y attribute from being initialized or updated as/to True if object a's x attribute isn't.
I guess if I'm concerned about that condition upon object of class b's creation, then I need to have a custom manager π€
can you create a constructor in class b which would check if a.x is true
It looks like that is the way to go with https://docs.djangoproject.com/en/4.0/topics/db/managers/
If someone has some good CSS skills, I have one main problem I just can't seem to crack. Hope I can share this link to share the issue? On mobile , https://OneHEP.com/build, my page is dealing with the issue where the VH is affected by the input bar at the top. It loads fine but then if you scroll down far enough to the bottom and then try to keep scrolling, the entire page slides up equal to the input bar height.
I've already replaced 100vh with 100% in a number of places after reading how it affects mobile viewports but cannot get this to go away. Anyone have insight into what aspect is still causing the bug?
:incoming_envelope: :ok_hand: applied mute to @inland oak until <t:1651005515:f> (9 minutes and 59 seconds) (reason: newlines rule: sent 132 newlines in 10s).
!unmute 370435997974134785
:incoming_envelope: :ok_hand: pardoned infraction mute for @inland oak.
Hi, please use the paste service (https://paste.pydis.com)
you can make it css-highlighted by changing the extension on the link to .css?noredirect
it looks like a typical reset problem, more precisely, lack of box-sizing: border box in your elements, including the input thingy. If all your elements would have this element, CSS will behave much more predictable
here is example of reset file that should be applied in the absolute beginning of all your code
Plus it will make the code more cross browser supporting π
I mean... that every element's size is combination of its own size + border + padding + margins. And due to different rules for each element in each browser
The size of every type of element is calculated differently.
That's why your 100% vh of some outter object is not really 100% in the end.
With reseting all elements to calculate their size with including border / padding (not remembering about marging)
the size of 100% or 100vh will be really 100% of the thing, and elements taking % or px of that size, will be taking precisely that size.
It will simplify for sure distribution of objects inside of another object.
Simple example which breaks the stuff when you aren't having reset:
You have a rectangle with 100% size
And you wish to have two smaller rectangles, taking exactly each taking 50% of space inside of the external one
If you don't have reset (and not having box-sizing: border-box.
The smaller objects will take size 100% + border + margin + whatever size, which will break all the... positioning.
With reseting and border-box, they will take exactly 50% size and filling precisely the external object
and yeah, it affects obviously flex elements too
Hmm, alright thanks for the insight, I'll take a look at that.
I assumed that resizing is performed dynamically and so that wouldn't matter.
Because as you change the window, 100% is a different size and elements usually do change accordingly.
Welp. Nope. When you set 100% size of the element, you set probably 100% of its internal size. But it adds border / padding / margins sizes, and woala. You have greater than 100% element.
With border-box, it should not be an issue. 100% will become real 100%
Any idea how to test for this locally before putting it online? Chrome tools don't have this issue.
so i am trying to webscrape this webpage: ```html
https://www.newegg.com/p/pl?d=rtx&N=4131 4841 4814 4021 4020 4019 4018 4017
this is my .py file:
```python
#https://realpython.com/beautiful-soup-web-scraper-python/ < tutorial
from bs4 import BeautifulSoup
import requests
page = requests.get("https://www.newegg.com/p/pl?d=rtx&N=4131%204841%204814%204021%204020%204019%204018%204017")
soup = BeautifulSoup(page.content, "html.parser")
results = soup.find(class_="list-wrap")
container = results.find_all("div", class_="item-container")
for containerElement in container:
brandingElement = containerElement.find("div", class_="item-branding")
titleElement=containerElement.find("a", class_="item-title")
rating = brandingElement.find("i", {"class":"rating"})["aria-label"]
priceElement = containerElement.find("li", class_="price-current")
#print(brandingElement)
print(rating)
print(titleElement.contents)
print(priceElement.text)
print()
i am trying to scrape this line:
<i class="rating rating-5" aria-label="rated 4.8 out of 5"></i>
i want to get the html rated 4.8 out of 5
and sometimes it outputs it, but for other items it outputs:
Webscraper Project\webscraper.py", line 13, in <module>
rating = brandingElement.find("i", {"class":"rating"})["aria-label"]
TypeError: 'NoneType' object is not subscriptable
and obviously when it gets to this point it stops the loop, but for some items it works perfectly fine
Can you find only rating-5
anyone know if these errors are okay? I'm making a simple webpage with a iframe to a youtube video
Heya I wanted help
Can anyone help me?
I did this
i get this error
i have installed werkzueg
@gray lance could you help me?
I want to find rating-4-5 and rating-5
This is a django question, Is there a way to execute a single action at the first migration attempt such as create certain objects in a model?
Hello people, can someone recommend an introduction to RESTful API (book)? Would be nice if it was based on python
Hello guys, any idea how to connect Vue frontend with FastAPI backend ?
you use the backend as an API and you access that API with the frontend
yes. I use fastapi + uvicorn +pymongo and it provides api and communicates with mongodb docker container.
But idk What should i do with VueJs in order to make a communication beetween FE and BE. I have heard that there are more solutions, idk which is the best
Probably using nginx ?
FE and BE?
frontend and backend
i'd recommend using the axios library to interact with the backend
you can use fetch too which is basically a default js library but axios has more features
thank you very much friend
can django render ejs files ?
<td class="hide">{{ computer["History"] }}</td>
.hide {
display: none;
}
.show:hover~.hide {
display: block;
}```
can someone tell me why this works, using 'sibling'. but if i use space it doesnt?
<td class="hide">{{ computer["History"] }}</td>
.hide {
display: none;
}
.show:hover .hide {
display: block;
}```
this doesnt work
flask and jinja?
yes, but im talking about hovering over the show, it should display the hide
just the css
yes
if i use the class name:
.show:hover .hide { display: block; }
it wont work
why is that?
i have to use
.show:hover~.hide
oh
may be use comma because they are two different class and no parent child case is there
for containerElement in container:
brandingElement = containerElement.find("div", class_="item-branding")
titleElement=containerElement.find("a", class_="item-title")
rating = brandingElement.find("i", {"class":"rating"})["aria-label"]
priceElement = containerElement.find("li", class_="price-current")
so this for loop checks for prices, ratings, and the name of an item on a website. it works. however, some items have no reviews, in which case it fails. how do i fix this? i was thinking of an if statement to check if the containerElement (the actual container the item and all its information is in) has a rating, but im not exacatly sure how to do that
You can do something like
if rating:
rating = rating['aria-label']
hello everyone π noob here,
i dont know how to go about this in css
i put an overflow-x: scroll; for that container in the red dashed box but i kinda want to hide it under the my main container
i figured it out
i did t his
if ratingElement != None:
rating = brandingElement.find("i", {"class":"rating"})["aria-label"]
print(rating)
and made a new element saying:
ratingElement = containerElement.find("i", class_="rating")
to look something like this
i finally was able to migrate the db from my todo app to my web app (flask)..now I have a web app where you need to register and the login to see the content and then you can use your to-do list to add todos, update them or delete them...my plan is to add some new features to it, such as email integration, etc.
I'm trying to build a simple web service that does auth through a magic link over email
Let's say the user types their email in and I send them a login link with some auth token
How can I securely tie that token with the browser? Cookies?
you don't have access to the cookies in the user's inbox. you have to store the token in your DB
okay, so when they make a request to perform a privileged action, how should they pass the token back to me?
as a query parameter in the request?
oh. deeeeeeeefinitely not as query params
after they click the email, you set something in the session, generally as a cookie which keys into the database, but honestly just do whatever your web framework prefers
(usually, the auth token is only valid once; the session key persists in the browser's cookie jar)
context: I'm trying to do really basic web app with a FastAPI backend where I can do auth strictly through email magic links to avoid the security risks that comes with storing passwords
there's no private info, no data collcetion, nothing to keep secret, so if I have to introduce passwords then the passwords themselves will be the biggest security headache
fastAPI is not really meant for webapps. looking at the docs, it doesn't have any built-in support for sessions. so you either need to invent sessions from scratch or switch to something like flask
I mean, I guess you can use fastAPI's openAPI support via api keys and then use fetch with credentials in your webapp................
but you'd still need to either accept that the token you mailed out is valid forever or turn the token in an api key for every browser session
eh, I might be able to cook something up that's simple.
flask is pretty simple. you're making things complicated by using a screwdriver to hammer nails
very true
I started with what I knew, and quickly stumbed into an edge case for something I don't
wouldn't be the end of the world to pivot
but also I've learned a lot about how all of this stuff works in the process
yeah, the webdev stack is really tall (and rickety)
I'm sure there are a lot of people who would shudder at the top, but as someone who does low-level stuff + backend work most of the time, it's kinda crazy how far I can get with just FastAPI + loading HTML + CSS + JS as string.Template objects and returning them
this... is what flask is for
fair
- if you're heavily invested in fastAPI (you're using api doc generation 'n stuff) and you want a static site that XHR's to your API, you can definitely get away with cobbling together something on top of fastAPI
- you can have both a flask app and an API running
- you can port your code to flask (which I don't think is very much work)
Should I just port to Flask? yes
Will I probably cobble together my own Session mgmt for the memes? Also yes. Flask docs say it's basically just a dict where you track edit time.
Thanks for the advice though.
@frosty aurora nooooooooooooooooo. it appears to the developer as a dict, but under the hood it's
- a thread-local so it knows which request (and cookies and user) it's for
- writing the session data either directly to a cookie or to the db keyed by the cookie
there's a lot to consider wrt sessions:
how do you make them tamper-proof?
can you load session data from another webserver (so your webservers are stateless and, therefore, horizontally scalable)?
i would disagree, there are lots of advanced people even in this channel that use fastapi for web apps as far as i have seen and also big companies
I mean so here's the workflow:
- generate and send an auth email
- user clicks email, I give them a session cookie, I keep a copy of that cookie for that user
- On my sqlite DB:
INSERT INTO userauth VALUES (:username, :session, :last_access, :expiry) - if the user is close to having a session expire, I do refresh and send a
Set-Cookieon their next request
Yeah a sqlite DB isn't "horizontally scalable" but if this project consumes more than the meager GCP VM that I can afford then I should find a way to make enough money to solve it later
on my load tests right now I can serve like 20k+ concurrent users on a GCP e2-micro and pump out hundreds of thousands of request a second
If I actually need to serve millions of requests per second and figure out how to handle state across multiple machines, then I'll solve then problem when it's, you know, a problem
but how do you map usernames to fastAPI tokens?
unrelatedly, how do you make sure your session identifiers can't be guessed?
make sure Session IDs can't be guessed?
Flask docs say to usesecretsfrom the stdlib, I'll probably just do that
how do you map usernames to fastAPI tokens?
My token will probably just be somesecrets.token_bytesencoded in base64, and my mapping from username to token is literally in the above query
SQLite handles inter-thread / process atomicity for me
So, at least for my very limited use case, seems like it'll be like 10-20 lines of Python given that the dependencies are doing most of the hard work for me
Anyways, if anyone does wanna roast my approach, I do very much appreciate the questions @hidden marten as it's very helpful
a wesbite for a business where it tells you a little about the business and the days that there open and there contact information does that count as a business landing page
I mean yes, but a "landing page" gets its name from being the first place that the user "lands" when they're visiting a website
you could have the π© emoji in 200 pt font as your entire website and if it's the first place I land when i type "http://your-business.com" then that's the landing page
@frosty aurora feel like a lot of details are being glossed over here. would be interested to see your implementation once you write it
I was hoping to come to this channel to find out what details I'm missing. I'm happy to share my solution when I get around to rolling it out
I'll probably publish the whole codebase under Apache eventually
Could someone help me with something like a chat for me and my feiends in school
a simple chat website
@frosty aurora ok, details:
are the emailed tokens API keys or keys you redeem for something? (and is that something a logged in session or an API token?)
how do you expire emailed keys?
how do you expire sessions?
I'm emailing them a link, they click the link, I give them a session token in their cookies
I keep timeouts in my SQLite DB
for session keys
I could probably just do the same for emailed links
I expire them by doing a pass over the table like once a minute or something
If the DB really gets to be a hassle to deal with, I could just spin up a memcached instance
actually... I might just do that
when you say "emailing them a link", what's the link? how do you associate the link with a user?
sure, timeouts are in the DB, but how are you expiring them? do you have a thread running in your app? do you have a cronjob running separately?
not sure what memcached is doing for you. normally you use it to make things faster by caching stuff. what are you caching? you wouldn't want a server restart to log your users out
I am caching sessions
I suppose a server restart logging everyone out isn't the most convenient
if I go the SQLite route, then either of those approaches is fine, but I'd probably lean towards just doing it in a thread
well, to be specific, I'd just spawn an async connection the aiosqlite and check on it
but a cron job could also do the trick
This project has too little functionality to be getting rebooted constantly
so the idea of people getting logged out isn't huge
servers reboot to take kernel upgrades, when the power goes out, etc.
fair enough, sqlite it is
so what are you emailing users?
let me just write the code snippet rq lol
Has anyone here created a interactive webapp for their machine learning model? I trying to do that but unsure where to start.
are you leanings towards signed and encrypted jwt:s with an encoded expire date in them to both scale and have a stateless server side?
I was considering signed, unencrypted keys
Is anyone awake that's familiar with Filezilla and Notepad++?
def generate_login_url(username: s):
sendtime = datetime.utcnow()..strftime('%Y-%m-%dT%H:%M:%S.%f%z')
utf8_token = username+sendtime
hashed_token = hashlib.new('sha256', utf8_token).digest()
b64_token = base64.url_b64encode(hashed_token).encode('utf-8')
return f"mywebsite.com/login/?token={b64_token}"
probably works just as well, I was thinking making the email addres a encoded part of the identity in the jwt to make the database server side unnecessary
(the 3rd line is a str, which is unicode, not utf8)
and then you're gonna store this hashed token in the DB?
I can store the unencoded sendtime in the DB and then regen the hash
or store the token
same thing
yeah, encoding creation time instead of expiration time is probably better
jwt:s are generally not great if you have to be able to invalidate specific sessions at any time without advanced notice, but for this I think you would be a good fit
the goal here is to have dead-simple, easy to debug login auth for a simple toy "social media project" that doesn't store PII and very very much isn't supposed to be used for anything important.
There are literally two features that require privileges
and they both have to do with uploading content, which 90% of a userbase never does
speaking of jwt:s and other things that are cryptographicly signed... i wonder how many enterprise, financial and government sites and systems that are vulnerable to generating ones own skeleton key due to CVE-2022-21449 π
not just that exploit
lots of sites are super vulnerable to lots of them
plenty of sites are still running on windows server 2003
One of common solutions is to invalidate jwt's that were issued before some date, for specific tokens you'd need to keep blocklist for these tokens
yeah, i know, doing it by date is the easy and standard method
while doing it per token necessitates keeping a shared state among all instances and services that reads the tokens, partially mitigating one of the major advantages of jwt:s
It depends on your system design really
You might have one central gateway that proxies requests to all other services and handles things like authentication
of course, i was thinking in terms of distributed multi cloud environments and other such distributed systems where jwt:s has huge advantages compared to many other solutions, drawing from my own experiences
Yep, that approach won't work unless all of your services are inside of a private network
that's why we are now using kind of a hybrid solution
we use old fashion sessions on our jwt issuing service which only issue quite short lived jwt:s (60 seconds) to clients
the clients (web browsers and mobile apps) then renew there jwt:s at regular intervals or as needed to access other services
we can't invalidate them instantly, but for our purposes one minute life latency in logging out a user is acceptable
i think others do it like that too
You use RS or HS?
won't say, they both have there pros and cons π
Using asymmetric algorithm for JWT's won't really have any cons?
Otherwise you need to share your secret across all services
Ayo!
Doctor!
Nice to see you here π
Can you take a look at my lollipop?
My lollipop needs some Doctor attention
Hi, I'm trying to figure out how to connect an app with IIS. Do I setup a web.config file to attack the scripts to?
just me thinking of RSA vs ECC, not HMAC, my bad
of course we use asymmetric encryption
we would not like all our services to have the signing key distributed
that only our jwt issuing service should have access to
Do you split your jwt issuing/auth service and users service?
That stores actual user information
yeah, why?
Just asking, a friend of mine said he would rather merge them, imo they have different responsibilities
and what i was referring to before is that RSA and ECC has there own pros and cons
mostly to ECC:s advantage... unless lately if one has been running java for any service that validate the jwt π
the only other "advantage" i can think of with [the dinosaur] RSA over ECC is maybe that it's battle tested for an extensive time
so, just a mix up on my side between HS and ES for a short while, don't think i have ever used HS, not even for a PoC or toy project
yeah, i agree with you, they have very different responsibilities, at least how i see it
maybe he should think long and hard about it before he goes ahead and merge them
How would you authenticate user by it's credentials in this case? π€
You don't want private information like password hashes to leave users service, so you'd probably need to create separate endpoint for that? Or would you create a separate service? (That seems to be a bit too much)
how do you mean?
we have a separate user authentication service in our hybrid approach
as the jwt issuing service use the normal session management before issuing each new jwt
i see what you mean now, but yeah, if you have a huge ecosystem it isn't that much extra to tuck on to it
I meant that if you have to authenticate user by username/password pair then you need to do the actual check in users service, so users-auth would make http request to users service to make sure provided password is valid
And to not share password hashes across services
Right? π€
@serene prawn You're not retrieving actual objects from your db - could you share some more info?