#web-development
2 messages ยท Page 29 of 1
Just a heads up for Django devs -- we've got a new User Authentication guide ๐ฏ https://kite.com/blog/python/django-authentication/
Anyone know how to download or screenshot all liked Instagram posts?
Automatically of course.
Preferably both, download the image and screenshot.
I'm not sure if Instagram allows that, but if it does then web scraping with beautiful soup should work.
I am working on technical test in python, and seems like its a base for simple queue-processing app. I was mostly working with other languages before.
What are usual ways of putting things to work together in python? is dependency injection common? any particular lib? is repository pattern for db common here?
are interfaces and abstract classes common?
@split mirage pyppeteer library will do that for you, dowload and screenshot. fast and clear.
@fallow herald Awesome! You think Instagram might get mad or shut down my account?
Maybe even shadowban me for acting automatically?
@split mirage instagram doesnt allow crawling
Web scraping instagram is against instagram's TOS, and so it also breaks rule 5 of our server, thus we will not help you with that or facilitate any such discussion here. thank you for understanding @fallow herald
!rule 5
5. We will not help you with anything that might break a law or the terms of service of any other community, site, service, or otherwise - No piracy, brute-forcing, captcha circumvention, sneaker bots, or anything else of that nature.
Hey, so I have a wordpress install on apache localhost:8000 at root, and wiki on root/wiki. When I try to access the wiki via site.com/wiki it works, but when I click a link from wordpress, like site.com/wiki it reformats it to be site.com:8000/wiki and breaks the link. I'm using an nginx reverse proxy infront of apache, redirecting all port 80 traffic to the localhost:8000 where apache is. How to fix?
do you guys have a preference between uwsgi vs gunicorn and why ?
hey guys, how can i make a query that filter selected tracks for this django model:
class Chart(models.Model):
title = models.CharField()
....
class Edition(models.Model):
chart = models.ForeignKey("Chart", ....)
....
class Item(models.Model):
edition = models.ForeignKey("Edition", )
object_id = models.UUID(db_index=True) #Track Object
i tried to get this by doing this:
c = Chart.objects.get(id=1)
list_ids = [i.object_id for i in c.editions.last().items.all()]
but it seems very dull idea to get. Like is there some sort of way to get it as a TrackObject?
@warm narwhal
Gunicorn just because its so frickin easy to start a server
I recently had to do a soft computer reset and had to reinstall python and flask, now when I try to run flask locally I get the following error.
Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
I don't remember having to set up environment variables or paths the last time I had a local dev server running
I'm using command prompt in windows 10, navigate to the directory, do set FLASK_APP = flask_app enter then flask run
^building on that, what does FLASK_APP look for, and how
a variable named app in an app.py file
does it call app.run?
but you can just specify your own location so you don't need to understand in depth how it works
Cause my issue is I don't have the Flask variable in the applications entry point
I start it through a manager
wdym a manager
app
|_ site
| |_ manager
|_ main.py``` ```py
manager = Manager()
if __name__ == "__main__":
endpoints = (dropdown, fill_in, multi_choice, data_endpoint)
manager.load_api_resources(endpoints)
args = parser.parse_args()
manager.run(debug=args.production)
class Manager:
def __init__(self):
self.app = Flask(__name__)
self.api = Api(self.app)
self.db = MongoDb()
self.log = logging.getLogger(__name__)
self.log.debug("Manager initialized")
def add_from_dict(self, dic):
for endpoint, class_ in dic.items():
class_.add(self, endpoint)
def load_api_resources(self, endpoints):
for endpoint in endpoints:
self.add_from_dict(endpoint)
def run(self, debug):
self.app.run(debug=debug)
Is basically what I do
It might be silly, but worked locally running it with python directly
run() is fine for local testing but you need a proper wsgi for production
.e.g gunicorn
I'm not sure if you can specify the flask app as an instance variable of another variable, like manager.app
Yeah, that part is completely out of scope for my current brain.
So something like this? ENV FLASK_APP app.main:manager.app
yeah
I think subclassing makes more sense
You could also do it like this https://flask.palletsprojects.com/en/1.1.x/tutorial/factory/
(though I wouldn't put the routes in there)
It looks like you could simplify that by using blueprints
But what I meant was, for the example they give, I wouldn't have defined my routes inside the factory
It's meant to be a simple example so fair enough
Might be far fetch, but what if i just made the repo public.
https://github.com/tagptroll1/INFO132-Sommer-Prosjekt-Server
How far out swimming am i for a proper flask (rest) server
The code itself looks close to done but I didn't spend a lot of time analysing it all
But you seem to have nothing in terms of deployment
you said you got a mongodb container running right
Yup
At least, it didnt crash
No idea if it actually works since i never got a flask container going
What you could do next is create the docker compose file
version: '3'
services:
web:
build: .
ports:
- "5000:5000"
mongo:
image: "mongo:3.4.22"```
?
ok cool
Lemme push what i have atm to a new branch
If only my internet would be stable, i could actually push it as well :|
I have this at least ```
FROM python:3.7-alpine
WORKDIR /code
COPY . .
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
ENV FLASK_APP app.main:flask_app
ENV FLASK_RUN_HOST 0.0.0.0
CMD ["flask", "run"]```
Which seems to run a flask app, but no routes work
You're using pipenv so you should switch out the requirements.txt stuff
pipenv added a whole new level of i dont even know ๐ฌ
alright you can deal with it later then
did you switch out FLASK_APP for what you said earlier?
@olive wharf so I should just include that init and etc in my flask_app.py? that simple?
oh sorry
Yeah I'm pretty lost atm
@limber orchid all you should need to do is set the env variable to the correct value for your project structure
yeah this was the tut I was trying to follow
and it had worked for me before
maybe I need to go through the flask installation again, like start a new site and just transfer my code?
That seems somewhat drastic but if you want you can try
I'm not sure what the issue is
maybe try a different terminal
like git bash
or powershell
I did try powershell, it produced a different error about my app not being a recognized thing
Do you recon i should just subclass Flask with my manager?
@olive wharf No routes though? I don't follow how you are adding routes really. I'm just used to using a blueprint but to be honest not experience there cause someone else set up a blueprint for me. My suggestion is either try subclassing instead of having the app as an instance variable or add the routes differently
My routes are based on this py class ApiBase(Resource): @classmethod def add(cls, manager, path): cls.manager = manager cls.database = manager.db manager.api.add_resource(cls, path) So all endpoints inherit that
And i just call add with the app and a route in manager
It was a way to help the others on the team be able to add routes
ยฏ_(ใ)_/ยฏ
Lemme try the subclassing way and see if that changes anything
Another idea is to create a basic route and see if it works
@limber orchid The error says that the environment variable wasn't even set, I don't know why. If you rename your file to app.py then flask should be able to automatically figure things out
So it runs, but i still get 404 for all my endpoints :/
What about if you create a basic endpoint
without all the complication of your system to add it
ok I'll try that, thanks!
To be clear the server is live right?
Well I guess you would get something other than a 404 if it wasn't
It does say so, in the terminal as well
It shows the 404 in the debug prints
and GET requests to the correct routes
:| Run the whole build ... Whops typo, here we go again building
manager = Manager()
class Test(Resource):
def get(self):
return {"hello": "world"}
manager.api.add_resource(Test, "/")``` works
Have you tested the app locally before? Were your endpoints working then?
Yup
Its 100% working locally
when i run it through python directly
But i have an idea, lemme rebuild really quick..
ok well you have a main.py
and that's calling py endpoints = (dropdown, fill_in, multi_choice, data_endpoint) manager.load_api_resources(endpoints)
Seems you rely on that to make your app work locally
presumably your testing locally via main.py
yup
I don't think I've set it up either
You can make the dockerfile invoke main.py instead
But this wouldn't be a long term solution if you plan to switch to a proper WSGI
cause those want a thing similar to FLASK_APP
so in such case the solution would be to move the code in main.py inside like Manager.__init__()
You're calling run() inside that
which you wouldn't want to do if you're using flask run
Yeah, but I'm also adding my routes there
So if i move those outside..?
~Waiting for build
I suggested putting them inside Manager.__init__
I am not sure you could put them after manager = Manager()
they may not get executed correctly ๐คท I don't know how flask loads the app
I thought for that reason they suggest using a factory
I believe the routes are working now, but I believe I'm missing a connection to the mongodb ๐ค
Yup
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
Containers on the same network should be able to talk to each other
And they would be on the same network by default I believe
but pretty sure not on localhost
Believe it needs to be the container_name:27017 ?
Docker networking is outside my knowledge tbh but I can try to dig something up
id have to read through this when i have more time, but it seems like a good place to start
yeah that link looks good
by the way you can also specify environment variables in docker compose, that will come in handy for you I think
it's envirinment: KEY: value in the compose for env variables right?
That's what I was looking at, as all connect variables for my mongo setup have env overwrites
Yes but you misspelled it ๐
Hi, I have apache and flask running, but when I try to import my py files into my main py it causes apache to just load forever and not display anything
Do i have to rebuild if i only change docker-compose.yml ?
no
What about gunicorn and nginx ๐ค
I frankly don't even know what they are.
nginx is a web server, gunicorn is a WSGI which is basically an interface the server uses to communicate with your app
Aha
Hi guys. Could you please point me to some example of production-ready, good quality cli/web app project structure? That would have all the fluff with all details like configs, dependency injection, containers and stuff? There seem to be tons of tutorials out there but all focused on one or two very specific things...
something that you consider an example of good quality and best practices
Might be farfetched, but I found this earlier in my look for answers. Seems to be a pretty solid template with lots of stuff https://github.com/wemake-services/wemake-django-template/tree/7882bcf89ad10d88f6624fc1d9b9ab7822175f0a
(but for django)
Was hoping for flask but django will certainly do. Thank you, kind stranger:)
@proper hinge got docker-compose to fire up a nginx and it seems to work
what's the next step? ๐ค
https://github.com/tagptroll1/INFO132-Sommer-Prosjekt-Server/tree/docker2 Is what I have going so far
You didn't need to create a network
Also the web service doesn't need to have a volume for the code cause the code was copied inside the dockerfile
hm
I may need the network, when i plonk the frontend code into this somehow
unless I can run 2 docker containers on linode?
No idea, I've never done this before
Cause I have a node server that needs to be deployed as well
Yes you should be able to run them all on the same server if you want
Yeah
Do i just need to call the flask callabel with gunicorn? ๐ค
well no you also need to configure nginx
Oh, well then im lost :D
I was just going to change this in the web service command: gunicorn -b 0.0.0.0:8000 app.main:manager
and add RUN pip install gunicorn in the dockerfile
:(
I tried to once and was scared off
ouch
This is their example https://docs.gunicorn.org/en/stable/deploy.html
Big boost in my confidence :D
I'll move to #414737889352744971 As it's probably more related to that at this point
I never really did web development in the past, some html,css,and javascript but been a while
I would look to make a project similar to this : https://david-peter.de/quizzity/
Where should I start? I have no idea where to start/learn etc... Totally lost
Any advices, tips?
That site is open source so you can take a look to see what technologies they used
Normally people say start with HTML, then CSS, then JS for the front end
In terms of learning I mean
I learned html and css but don't remember much, do you know any sources to help me learn and have a good base to start working on the project I wanna do?
@proper hinge
I don't know if it's particularly good but I recall seeing a tutorial on MDN
I'd recommend learning Flask
start with just serving simple web pages, then add some css to make em pretty, then add JS to add fancy buttons & stuff
then learn about DBs, session management
how can Flask help for that project?
AH nvm, that looks very JS heavy
I mean it just want something with like a city and you can click parks, or the main streets of the city, things like that.
Something similar to the link I sent, or like this also : https://online.seterra.com/en/vgp/3209
is that also JS heavy?
Theoretically yes
like that last example you gave is essentialy an SVG image with lots of JS watching what you click on it.
and is that too hard for a beginner? I don't mind taking a lot of time to do that, just wanna learn in the process
oh I see
the david-peter thing is a lot more complicated then?
Yes, I'd say so.
You will need some backend setup too. HTML/css/JS is all front-end stuff.
The 2nd link's svg looks programatically generated somehow, and thats done on thebackend.
It might just be a library or something
If you're a beginner though, this stuff is really complicated. I've been learning for a few months and can't do stuff like that ๐
oh okay haha, it's too complicated (the first link? or for both links?)
yeah the first one is using a lot of libraries too , in his readme it says : leadflet, lodash, jquery, font-awesome, animate.css, GeoNames, Natural earth, Tilemill, python-mbtiles
''You will need some backend setup too. HTML/css/JS is all front-end stuff.'':
backend stuff would be the database for all the information about the cities and stuff?
yes, and also serving your html/css/js
websites need a server after all
(of which I would recommend you start with flask, because its super easy and you can mostly ignore backend stuff if you prefer by keeping it super simple, like 10 lines of python code)
It can process requests the server receives and render an HTML page from a template for example
Or you can write an API with it
Sometimes you need processing to be done on the server rather than on the client side
oh I see, but not sure how Flask can help me make the geography thing?
you need a server to send that web-page to people
flask is a simple server
you can write all the html you want, but its gonna be useless if its never served
so basically I need Flask + html + css + JS to make all that possible?
If someone goes to your website, flask is gonna be like "here, take all this html/css/JS"
Yes
you don't need flask, but its probably the easiest
yeah sounds complicated for beginner, idk if I can do that, might try though
could use django, or if you want to go non python, PHP
They serve similar purposes. In my opinion, django is more heavyweight and more opinionated, and more complex. However, it does not suffer from some of the limitations and provides a thorough framework that guides the building of an application.
For your first project I would highly recommend flask over django, as the limitations of flask are unlikely to bite you, and understanding view/model/admin, migrations, and database is often overkill
What are the limitations of Flask vs. Django though?
Flask does not come with built in orm, an extensible and complex admin interface, built in module handling, settings interfaces, etc
I need help with Web development.
!ask
Asking good questions will yield a much higher chance of a quick response:
โข Don't ask to ask your question, just go ahead and tell us your problem.
โข Try to solve the problem on your own first, we're not going to write code for you.
โข Show us the code you've tried and any errors or unexpected results it's giving
โข Keep your patience while we're helping you.
You can find a much more detailed explanation on our website.
I am thinking of starting my project and I doubt that Django supports Bootstrap!!!
They are compatible
K
What does django have to do with bootstrap
django does support bootstrap right?
yes
bootstrap(tmpdir=tmpdir)```
It's just a short piece of code for setting up bootstrap
anyone here familiar with django and/or mezzanine that could give me some help with something? I am trying to make a new page model in mezzanine inherited from the default page model, but I can't get my new fields to show up on the webpage, only the fields that are inherited by page ๐ฆ
I created my new model by adding this to my theme app's pages/models.py: https://pastebin.com/caxBAUNJ
This does work and actually gets those fields to show up in a new page type selectable from my admin control panel, but I can't get any of those custom fields to show up on the published page, no matter what i have tried so far.
You can ask a link for pastebin. They provide you help with everything. I would recommend to ask in #help-coconut for pastebin link.
And yes I saw you used pastebin.
I'm sorry, what?
One admin is on #ot0-fear-of-python you could ask him.
yeah i dont feel the need to paste this across several channels
and people in the django discord be like 'i dont use mezzanine'
and the mezzanine irc be like (wind sounds)
what is mezzanine
mezzanine is a kind of ready to go framework for blogging over django
its django with moar templates
I would use zeta
some small diffs but not much
but I am new to all of it and am using this as a sort of educational process for myself
oh django cms
I do websites with C# even though I suck
yuss
you're more likely to get help with mezzanine people then django peeps tho
since it's build for django
mezzanine has only an irc channel with about 0 active members
:/
been like that for years
yea that's why community matters
That is obvious
goodluck tho
yep. django is already kinda poorly documented, assumes you know a lot more than most do
It's got a bit of a learning curve alright
mezzanine is even worse because its just like "oh, go look at the django docs for more info"
TaskScheduler.FromCurrentSynchronizationContext());
why are you posting these things that add nothing to the discussion
Idk
I know my issue lies in one of two problems. Either I need to explicitly register those extra fields somewhere as existing
OR
I don't know their path.to.the.fieldname
what error do you get
Wait, can you send the pastebin link again?
did u migrate?
yes i migrated
heres my models.py
all those extra fields like ATP ATA etc, are present and accounted for in the admin page when creating a "weapon page"
but i cannot get them to show up in my template
I am stupid or is it true that you could "" on strings?
it works
the only issue is not being able to call up these things in my weaponpage template
btw, if you are curious of the terminology, i am trying to put together a database of weapons and armor from an old RPG game
why are you inheriting from both page and rich text
its django with moar templates
moar powwwa
or is that the workflow
I just copied what they did for adding richtext page types
to be safe i inherited the same stuff.
within pages, mezzanine has default:
link pages
rich text pages
and some other
I am basing my custom page off of the mezzanine rich text page template
thank you very much. I feel i am very close.
See in my weapon page template I have {{ page.weaponpage.content|richtext_filters }} and this causes the page's main text body to appear.
but {{ page.weaponpage.atp }} comes up with nothing, and no error either which i found weird
figured it out with help from another server, not sure what i did wrong but trying page.weaponpage.ATP worked. Its case sensitive (i dropped the capital letters now). Weird because yesterday i tried with ATP and it still didnt work. Guess i just needed to sleep haha
Does anyone know of a good way to check if Redis is running, but no workers?
The final line succeeds if there is a worker, but fails if there is not, but obviously it's not a good solution since it depends on the sleep
task = self.task_func.apply_async()
time.sleep(3)
print(task.info.get('current', 0))
is there a way to have a folder with a bunch of markdown files.
And that folder is named like blog or smth
and so when you go to mywebsite.com/blog/subpage it displays the markdown file with the name subpage.md inside of the blog folder.
I think you could have some kind of python parser file using flask but I am not sure.
Essentially I want to be able to create markdown files, directly into the folder, upload it, and boom, html page created.
Another functionality I would like to have is someway to extract variables stored inside of the markdown files, such as:
subpage.md:
[blogpage-name]: My Blogpage Name
I don't remember if that's how you create vars but point remains.
This way I could get stuff like the title of the blog, date that it was published etc
from tkinter import *
#Python default bank sample
from random import *
Bank = Tk()
Bank.config(bg = "grey")
Bank.geometry("1350x800")
Bank.title("")
That's a basic tkinter sample
Currently it's a bank program sample
Hi all.
I have a peculiar question about Django admin
Trying to validate an object before it's being saved with custom "save_model" function.
There is a list of inline objects and each of these inline objects has a boolean attribute
I'm trying to raise an error if more than one of these objects values is set to true
Can someone help me sort it out please?
Can you give me Pastebin link please?
A flask question:
is is safe to edit request.data ?
eg, for example, a decorator that gets request.data and then sets it to decompressed data
scanning the docs, it seems like it should be, because flask.request is thread-local, and it can't be passed to other threads anyway
hmm. maybe not. i think request is a werkzeug proxied local
Acts as a proxy for a werkzeug local. Forwards all operations to a proxied object. The only operations not supported for forwarding are right handed operands and any kind of assignment.
hmm, how should I design this kind of decorator then
and yeah I'm asking cause PyCharm is telling me it's not assignable (but it works)
whats your current code?
def accepts_compressed(f):
@functools.wraps(f)
def view(*args, **kwargs):
if request.headers.get("Content-Encoding", "") in ["gzip", "deflate"]:
request.data = zlib.decompress(request.data)
return f(*args, **kwargs)
return view
basically I wanna do smth like this
(maybe not exactly decompress, but some data transformation)
I guess I could just do a get_data() function
and call it at beginning of each view
and call all processing needed from that func
hmmmmmm. yeah thats a good question. herein lies the danger of using mysterious magical global objects
i think your get_data() idea makes the most sense
Can someone help
I'm creating a form with Python, Bootstrap 4, Flask & Jinja2
I want to change the input to have placeholder text and for input background to be black instead of white.
<h1>CONTACT</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Impedit voluptates minima cum odit eligendi, ut fuga suscipit dicta harum repellat assumenda dignissimos! Ad consequuntur ipsam deleniti quidem, quos obcaecati alias.</p>
</div>
<div class="container col-lg-6">
<div class="content-section">
<form method="POST" action="">
{{ form.hidden_tag() }}
<fieldset class="form-group">
<div class="form-group">
{{ form.name.label(class="form-control-label") }}
{% if form.name.error %}
{{ form.name(class="form-control form-control-sm is-invalid") }}
<div class="invalid-feedback">
{% for error in form.name.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.name(class="form-control form-control-sm")}}
{% endif %}
</div>
<div class="form-group">
{{ form.email.label(class="form-control-label") }}
{% if form.email.error %}
{{ form.email(class="form-control form-control-sm is-invalid") }}
<div class="invalid-feedback">
{% for error in form.email.errors %}
<span>{{ error }}</span>
{% endfor %}
</div>
{% else %}
{{ form.email(class="form-control form-control-sm")}}
{% endif %}
</div>
Anyone have experience with using django to send push notifications to ios devices? I'm trying to attach an image to the notification and need a nudge in the right direction, because it seems like my payload json is structured properly.
Doing that is a very long process.
Just adding images to the notifications? I can push simple alerts to iOS and 'rich' notifications work on Android
There isn't much documentation on this online so you're probably right
https://kiranvaag.itch.io/cataratastrophe - Can any1 give me feedback?
is there a way to have a folder with a bunch of markdown files.
And that folder is named like blog or smth
and so when you go to mywebsite.com/blog/subpage it displays the markdown file with the name subpage.md inside of the blog folder.
I think you could have some kind of python parser file using flask but I am not sure.
Another functionality I would like to have is someway to extract variables stored inside of the markdown files, such as:
[blogpage-name]: My Blogpage Name
So lets say I have a folder called "blog/posts". Ok so htmlcssjs files are totally static, I don't change them ever. What I do is simply create a blogpost in markdown, upload it to the blog/posts folder. Then, the blog homepage search functionality is able to let you search for it, the "sort by date released" also is able to put it at the top, including its name title (extracted from variables) etc. Then when I go to the correct page, it will look for the markdown page, render it, and show the blog post that I made, all automated. The ONLY thing I do is create a markdown file with variables such as the name, author etc. and upload it to the folder, everything else is automated.
I am really interested in web development but im kinda a beginner in python at what point is a good point to start using django?
I would recommend flask
To learn how to figure stuff
In django you do stuff only with one way
In flask you can do stuff with 3/4 ways
Being limited to one good way is often better than being forced to figure out the best of many ways. A rigid structure I think is more useful when starting out, especially as the Django docs include a pretty good tutorial
But do i need a good understanding of python or can i just start now?
I would say learn how functions work
i understand how they work
Since web dev is just a bunch of functions connected to some task
maybe not the most complex ways of them but i can use them
Then i'd say you are ready
alright thanks
I strongly reccomend flask
What is the difference? between flask and django
understanding classes is important too
I ran into a real bad issue friends. Long story short I've been using Edge browser, now my last session will try to open and then edge immediately crashes. This is a known bug that Microsoft never fixed. I would be happy to find if there is a list of URLs from the last session of about 200 tabs somewhere on my computer. Anyone know where to look?
If not then perhaps there is a session file that I can export to a different computer to see if the session will open on that one.
Can Microsoft sync Edge browser sessions between computers? If so then that might help if I can open it on another computer.
this isn't web development related
but I would try right clicking on the icon and scrolling to the history, then selecting a tab that isn't the one that breaks your browser
I would also reccomend ditching edge
honestly, why do you use that of all browsers out there.
Hindsight is 2020, I started using Edge because Chrome won't allow you to scroll through your tabs. I open hundreds of them. My last ditch option is to open a private edge session which so far is the only way to get edge not to crash upon starting and then go through the past 3 months of history and try to piece together my last session of 200ish tabs. Not ideal.
scroll through tabs? chrome allows you to
can't you go into the settings and say "do not reopen last session"
I know chrome, if it crashes, saves the entire session you can open in one click
The tab bar at the top of Chrome? No they squeeze together, then at some point when you've opened like 300 tabs you can't click on the tabs off screen.
Edge is not cool like that, it doesn't even have a session managing extension.
Gg mate
Hey Guys, I have a problem with my django app. I am in a production environment, when my debug is false i can't collect any files. I already deployed an app in django before and didn't encoutered this problem.. I did collectstatic but still not working. The only thing i changed compared to my previous project is I added an ImageField with upload possibility for the user.
I have almost the same issue than this post https://stackoverflow.com/questions/57282125/django-2-2-staticfiles-do-not-work-in-development
I'm setting up a relatively simple REST API (at most 4/5 different tables in the underlying database) - is using Flask-SQLAlchemy bad?
Problem solved... noob mistake, staticfiles not defined on my nginx conf ๐ข
@rigid laurel sqlalchemy can handle way more than 4-5 tables, it's literally only good SQL ORM for flask
better than writing raw SQL xD
I wasn't wondering whether it was the best ORM, rather if it was the most efficient way to write a sensible REST API
well not sensible, its obviously sensible. Rather a good rest api
yes, it's more than enough
what are good web projects that I can do in Python,
also Django or Flask ?
Image converter ?
yeah
Can you explain a bit deeper what that program is supposed to do ?
From higher to low res ?
effects ?
i send .jpg to a backend and you return me a .png
Oh, so frontend just has upload file and button that triggeres backend magic
And in that backend its supposed to do conversion using module ?
yes
then after that ?
I want something that I can work on during school year
and until my school starts
umm
so
try recreating one of big websites
like reddit
try to make a reddit clone
oh that is damn huge, anything a bit smaller ?
I would like to code apps, it doesn't need to big since I will be picking up a lot of DevOps
be too big*
hmm
quora,reddit,twitter
all follow the same principle
try twitch
or google drive clone
yeah
or my server
anyways what about a blog ?
I need some kind of personal portfolio for my $h1t, I will probably host it on AWS or GCP
How would Flask be for maintaining things ?
Does it require attention or I can slap it on NGINX and hook up CI/CD
with bunch of tests
also I would probably do double database (Sounds stupid a f but more DevOps practice for me);
SQL for logins and registration + titles of posts
MongoDB containing content of posts
I want to build a web app. Which one is better to do that flask or Django? (Iโm stucked)
@native tide
you can connect it to some CI/CD no problem
also, you are gonna struggle a LOT with separate databases my dude
So just SQL ?
yeah
alright
fast af boi
It lacks features that Gitlab has k8s support, yaml CI/CD pipeline and ability to have local install on your server
I think Gitlab is much better but I don't want to use public ones cause I am afraid of censorship like it happend to Iran
Why not NGINX in VM on cloud ?
I like idea of load balancing stuff
One server in west europe and one in east asia (japan probably)
I would use that
And have load balancer
on eu-west-2 and ap-northeast-1 instances on AWS
also, you can have unlimited private repos on gitlab
I know that
I just like using local install, I'll probably hook up CI/CD to clone it to Gitlab
๐
I would be pissed if my account god erased cause of politicians
Like with Iran and Github
lol
Imagine work that was wasted, all those PR's, Issues and Wiki's
I would generally be pissed af
cya and thanks 4 help
ping me if you have more questions
k
whats the most efficient way of getting a user based on the active domain? foo.bar.com, bar.com, www.bar.com all indicating user ~4 is the account to pull preferences from? i'm using latest python/django.
?
Is php included in this channel?
If so
Can anyone help me with making a video sharing website (eg. youtube, vimeo, daily motion, ect)
@vagrant adder Hey, quick question, what else RDBMS should I learn except PostgreSQL, I've been wanting to get into more systems alongside PostgreSQL like MS SQL or Oracle Database ?
Are they all same in terms of features or they are different
Nowdays, postgres is GOTO
You should really use it except you have a BIG reason not to
Maybe learn some NOSQL
Firebase/mongo
MongoDB ?
Tbh I am bigger fan of SQL cause of syntax and ease of data structure
and manipulating data in tables
Yeah, then postgres is a must
I know that
but is it worth picking up basics of MS SQL or Oracle DB ?
Both are free
oh ok
What IDE is good for supporting my selection of langs : Python, Ruby, Go, YAML and SQL
oh and JS
Firebase is used a lot in cloud hosting, and mongo in containerised applications
IDE?
Hmm
For go there isnt really other options than GOLAND
I am 16 year old still learning, hopefully I'll get enough money for Goland
and Pycharm
Alright,
I've been mostly writing in Vim (I don't like tbh and had paper with keybinds)
Lol
PyCharm is free
IntelliJ IDEA can support all those languages with plugins I believe, but typically some features will be missing compared to their IDEs which are specific to those languages
Probably it's the only IDE that can support all those languages
maybe Visual Studio? I know it supports some of those but not sure about all
How do I change the collapse background color in Bootstrap 4?
just change it normal in css
like you usually do
if it won't change, add !important tag
so
body {
background-color: red !important;
}
i like vim, but if you don't like it, use something else, by all mean, vscode is all the rage these days
if you know sql, unless you need to be an expert on a particular system, most sql things should be mostly the same, so i would care only if you try to get a job that use them. Sqlite is cute and used in a lot of systems (from phones to cars, to browsers, to well, everything, this thing is literally used everywhere), it's probably good to know enough to know how bad mogno is, and probably to try other schemaless databases, but sql is getting a lot of love again after people really overbought the nosql hype.
oh, and don't worry about being ยซstill learningยป, you are never going to be done learning ๐
@Void#0324 Please ping next time when you reply to a message that's 40 minutes old. I already gave up looking in this chat after ~20 minutes. Found solution myself but it took me a long time.
@ruby palm From how I get it, it's a grid system for elements on a web page. There are multiple systems like that one.
Hi all!
Need some help with setting up multiple languages in Django
First, I've given my training project multiple languages in settings.py
And now I'm following this guide to let user select their language of choice.
In one of my Django projects available only for logged in users I needed to set language setting in the database, so user can change the la...
So far I've only made it visible on admin side to test it out.
The language changes if I change LANGUAGE_CODE option in settings but won't change based on user choice.
Seems like I'm forgetting something but I cannot find it in the article
Can I get some assistance here please?
I think it may be related to my User model cause I have a custom one.
class User(AbstractUser):
"""
Custom user model.
"""
phone = models.CharField(max_length=100, unique=True)
username = models.CharField(max_length=150, unique=False, blank=True, null=True)
first_name = models.CharField(_('first name'), max_length=30)
language = models.CharField(max_length=20, choices=settings.LANGUAGES)
USERNAME_FIELD = 'phone'
REQUIRED_FIELDS = ['first_name']
objects = UserManager()
def __str__(self):
return self.phone
@classmethod
def normalize_phone(cls, phone):
"""
Remove extra spaces and non-digit chars from phone.
"""
_normalize_phone = re.compile(r'(\s{2,}|[a-zA-Z]+)').sub
return _normalize_phone('', phone)
@receiver(user_logged_in)
def lang(sender, **kwargs):
lang_code = kwargs['user'].language
kwargs['request'].session['django_language'] = lang_code
What did you encounter to know it's not working ?
Does anyone know what the best way to manage a postgres connection pool with flask is
And how to make it accessible from blueprint files
What do you use
I've created a simple login with Google using flask.
Since one day ago, I get this error when I try to log in.
('Request limit reached', 429, {'Server': 'nginx/1.10.3 (Ubuntu)', 'Date': 'Tue, 13 Aug 2019 07:01:58 GMT', 'Content-Type': 'application/json; charset=utf-8', 'Content-Length': '33', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET', 'Access-Control-Allow-Headers': 'Content-Type', 'ETag': 'W/"21-tYoIBroDGdB+35cIAOMCdpXfqjI"'})
@vagrant adder psycopg2
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
Is there a way to turn this into a class?
class FlaskApp(Flask):
def hello_world(self):
# ...
This is my code:
<div class="list-group-item list-dark-flush"><div class="picker" data-initialColor="{{color}}"></div> {{subject}}</div>
How do I align the "subject" div next to the "picker"?
This is what I get now:
I would set it to a child of it then do this ```CSS
#greenThing {
position: relative;
align-items: center;
}
u might have to set left to 0 and right to 0 too
float, or flex grid
Can someone help me create a contact page using Flask?
Hi broskis, anyone using pydantic?
Is there a way to convert this:
`class GetGamesResponse(BaseModel):
games: List[Game] = []
async def get_games():
output = []
for game in games.find():
output.append(game)
return GetGamesResponse(**{"games" : output})`
into this:
async def get_games(): output = [] for game in games.find(): output.append(game) return [Game](**output))
EDIT a solution came to my mind:
@router.get("/") async def get_games(): output = [] for game in games.find(): output.append(Game(**game)) return {"games" : output}
@tranquil steeple it's better to ask your questions directly, like "it tried x and y, and y does not work giving this error". You are much more likely to get a helpful answer that way.
@candid basalt thanks for the suggestion. Although, that wouldn't have serviced it was more a question of best practice. It required more of a conversation. I got the help I needed anyways. Thanks for taking the time to respond.
I'm having a difficult time with uwsgi timeouts after upgrading an app from Django 1.9->1.11 and Celery 3.x->4.3, and I think the problem is related to Celery but I'm struggling to debug and fix. Is this the right place to ask for help?
some things: i know that celery has all the tasks registered; i know that the server sending tasks can connect to the server running the broker & worker processes (tested via telnet but i do wonder if i need to further troubleshoot this step); tasks run fine in my dev environment in which celery/broker/webserver are all on the same machine.
i also know that (at least some) tasks run successfully if called directly and not via .delay(). perhaps checking the remaining tasks for this is another step i need to take
hi everyone, how do i know what is my superuser username and password in django? i clearly forgot my account ๐
@native tide if you have cli access you can use manage.py shell to find the username. The password should be encrypted and you probably will have to reset or something. Maybe just delete the old one and make a new super user from the cli
I have this code:
<div class="list-group list-group-flush list-group-horizontal-xl">
{% for subject, color in subjects.items() %}
<div class="list-group-item list-dark-flush" style="align-items: center; display: flex"><div class="picker" data-initialColor="{{color if not color == 'none' else '#03A9F4'}}"></div> <span>{{subject}}</span></div>
{% endfor %}
</div>
I want to make that list going in the next line after 4 items.
How I can do this?
(I'm using bootstrap 4 and flask)
This is what I get now:
anyone know why I keep getting 502 bad gateway on my flask app?
using gunicorn/flask/nginx
Sometimes it works, sometimes it doesn't
sometimes my other app routes fail because of the 502 bad gateway
In this Python Flask Tutorial, we will be learning how to deploy our application to a Linux Server from scratch using Linode. If you would like $20 of free c...
used this tutorial to setup nginx/gunicorn and the supervisor script to get it all running
also sometimes my socket connections dont work on my app
this only happened when i pushed to production
I am interning at a startup
There's an IoT device that sends data and a django web app that reads the data and analyses it
does AWS help with this?
or any other cloud service
Not really
@native tide are you using anything involving socketing or spawning new subprocesses
@native tide okay, I ran into a similar problem yesterday involving trying to use a subprocess in a gunicorn flask app. what are you using as far as service workers go?
also, have you installed anything like eventlet or gevent or something
yeah im using eventlet
hmm
echo "[program:domalert]
directory=/home/dom/domalert/domALERTV5
command=/home/dom/domalert/venv/bin/gunicorn --worker-class eventlet -w 1 app:app --timeout 90 --reload
user=dom
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
stderr_logfile=/var/log/domalert/domALERTV5.err.log
stdout_logfile=/var/log/domalert/domALERTV5.out.log" >> /etc/supervisor/conf.d/domalert.conf```
thats my supervisor conf
command=/home/dom/domalert/venv/bin/gunicorn --worker-class eventlet -w 1 app:app --timeout 90 --reload
from what i can gather (based on my problem yesterday), its happening cause gunicorn's master process is getting confused and intercepting signals from sockets/subprocess and treating them as if they are its workers
hmm ok
i havent come to a solution yet unfortunately.
oh damn
yeah it was ridiculous. have you tried looking at the system logs when the crash occurs?
like, the syslog file in /var/log
dom@domalert:/var/log/domalert$ cat domALERTV5.err.log
[2019-08-15 17:23:08 +0000] [17391] [INFO] Starting gunicorn 19.9.0
[2019-08-15 17:23:08 +0000] [17391] [INFO] Listening at: http://127.0.0.1:8000 (17391)
[2019-08-15 17:23:08 +0000] [17391] [INFO] Using worker: eventlet
[2019-08-15 17:23:08 +0000] [17394] [INFO] Booting worker with pid: 17394
[2019-08-15 17:24:22 +0000] [17394] [ERROR] Error handling request /socket.io/?EIO=3&transport=polling&t=MoMJkJI&sid=737e8ca231114691a46668dbd7e2f6e2
Traceback (most recent call last):
File "/home/dom/domalert/venv/lib/python3.6/site-packages/gunicorn/workers/base_async.py", line 56, in handle
self.handle_request(listener_name, req, client, addr)
File "/home/dom/domalert/venv/lib/python3.6/site-packages/gunicorn/workers/base_async.py", line 107, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/home/dom/domalert/venv/lib/python3.6/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/home/dom/domalert/venv/lib/python3.6/site-packages/flask_socketio/__init__.py", line 46, in __call__
start_response)
File "/home/dom/domalert/venv/lib/python3.6/site-packages/engineio/middleware.py", line 60, in __call__
return self.engineio_app.handle_request(environ, start_response)
File "/home/dom/domalert/venv/lib/python3.6/site-packages/socketio/server.py", line 534, in handle_request
return self.eio.handle_request(environ, start_response)
File "/home/dom/domalert/venv/lib/python3.6/site-packages/engineio/server.py", line 369, in handle_request
jsonp_index=jsonp_index)
File "/home/dom/domalert/venv/lib/python3.6/site-packages/engineio/server.py", line 566, in _ok
b64=b64, jsonp_index=jsonp_index)}
File "/home/dom/domalert/venv/lib/python3.6/site-packages/engineio/payload.py", line 19, in encode
encoded_packet = pkt.encode(b64=b64)
AttributeError: 'NoneType' object has no attribute 'encode'```
@midnight kernel
ah, gotta love when there errors are wonderfully ambiguous.
is it happening with every endpoint that uses the websockets or just specific ones?
ive read a bunch about how high cpu intensive tasks sometimes make all of the gunicorn socketing stuff start throwing a tantrum
i think in socketio theres a parameter you can pass to the constructor, like a logger param or something you can set to True to get more details on the websocket itself
hmm ok
i mean i just have one socket endpoint meant to establish a connection
and emit a event
i dont have the socket io reverse proxy in my nginx
have you tried that @midnight kernel ? also https://www.digitalocean.com/community/questions/502-bad-gateway-ngnix-gunicorn might help
Hey guys, I followed these: https://www.digitalocean.com/community/articles/how-to-install-and-configure-django-with-postgres-nginx-and-gunicorn intrstuctions to get my Django project up and running on my Droplet. Problem is I'm getting a 502 Bad Gate
i'm yet to try these solutions tho
yeah im pretty sure you need to configure that in nginx. my problem isnt with socket io specifically, but with spawning a new subprocess to call a bash script, but the cause of the problem seems similar (getting a socket error)
its possible that without the configuration, its sending the socket io request to the main listening port
yeah it seems like its trying that
becuase its saying it wasnt expecting a 200 response
i think only normal app routes return 200 reponses
yeah a 200 response is not something a websocket should return lol
in the 100 range is for websockets
(i think)
Has anyone explored integrating interactive matplotlib figures into Django?
@midnight kernel what does your nginx file look like?
probably a silly question, and I don't know if I should ask here or in #databases
do search forms that pop up suggestions perform a query to the database every time, or do they have a cache for this?
like this, I mean
I don't know much about databases, so if there was a cache for this I couldn't even guess where and how
someone firm with XPath?
I have a XML like this:
<Asset ID="PB_106682" UserTypeID="ProductImage">
<AssetPushLocation ConfigurationID="AP_SHOP_ProdImg_720">Produktbilder/720/PB_106682.jpg</AssetPushLocation>
<Values>
<Value AttributeID="asset.pixel-height" UnitID="pixels">531</Value>
<Value AttributeID="asset.size">1986965</Value>
<Value AttributeID="asset.format">EPS (Encapsulated Postscript application)</Value>
<Value AttributeID="asset.preview-format">TIFF (Tagged Image File Format image)</Value>
<Value AttributeID="AP_SHOPS">1</Value>
<Value AttributeID="asset.width" UnitID="mm">75,01466666666666</Value>
<Value AttributeID="asset.filename">106359.eps</Value>
<Value AttributeID="ATTR_AssetPush_site">1</Value>
<Value AttributeID="asset.mime-type">application/postscript</Value>
<Value AttributeID="ATTR_Asset_Marke">something</Value>
<Value AttributeID="asset.profile">Photoshop, ICC color</Value>
</Values>
</Asset>
and via an XPath, I want to get rid of every value, except the asset.mime-type
I've created a sortable list using jQuery UI's sortable function.
In my flask app I can get the order of the list by ajax but I don't know how to set the order on the html.
$(function(){
$('#draggable-cards').sortable({
handle: '.move-button',
axis: 'y',
animation: 200,
update: function(event, ui) {
var postData = $('#draggable-cards').sortable('serialize', {key: "id"});
$.ajax({
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(postData),
dataType: 'json',
url: '/_save_order'
});
}
});
})
@app.route('/_save_order', methods=['POST'])
def _save_order():
data = request.json
x = data.replace('id=', ', ').replace(', ', '')
y = x.split('&')
me = google.get('userinfo').data
with open('data/settings.json') as f:
settings = json.load(f)
settings[str(me['id'])]['order'] = []
settings[str(me['id'])]['order'] = settings[str(me['id'])]['order'] + y
with open('data/settings.json', 'w') as f:
json.dump(settings, f, indent=4, sort_keys=True)
return str('')
@tough star
if request.method=="POST":
saki = do_something(request.get_json())
print(saki)
return saki
That will return posted json back to html
Example:
- List element 1
- List element 2
- List element 3
With my code, if I move an element, I save in the json file the current order of the elements.
I don't know how to restore the order of the list (html)
Okay
I can sort the elements thanks to jQuery UI sortable
Okay
I can save the order after I sorted the elements
@tough star When you first load the page with the list, you want to retrieve the stored ordering from the server and render the list with that order?
Yes
This is my html code for the list: https://paste.pydis.com/gerehanawo.py (in this case the list is of bootstrap 4 cards)
For every tr you can make another for loop
tr?
table row
Let me get home then i'll try to make something
Ok thanks ๐
@vagrant adder Did you found something?
aight
you retrieve list order from backend and want it sorted
your best bet is to clear the parent div/table
and use javascript to rebuild that div/table accordingly to the order
But how? ๐ฆ
Anybody here know load balancing with Nginx?
I'm attempting to setup nginx conf to serve flask via uwsgi, but not sure what preferred way is.
A few tutorials have slightly different ways.
hi, all. how can i get django-simple-bulma to work in jinja2?
data = JSONField()
so with the models i have a variable set to that and if i want to send a PATCH request to the view for that how can i make it so taht it only updates a single area within that data instead of completely overwriting the JSONField?
I'm about to make my first website. I wanted to use Python. Wish me luck! So excited. Using the Flask framework.
u got this!
Thanks, @zealous igloo ! What are you working on?
Let's make the next biggest Social Network!!!
working on website with django that lets u store information and stuff on ur favorite tv shows it's mostly just for practice with http requests and getting feet wet with django
@remote anchor lol that's huge
Good luck on your TV show website! You'll be a professional dev in no time.
And we could TOTALLY make the next Social Media website. I wanted to make a social hub where it has built in translation and language exchange tools. I used to use an app called HelloTalk, but the management is off and I dislike the business model.
lol hopefully i can become a professional dev soon. adnd niiicceee sounds like good idea but really hard with the translation stuff
@verbal ibex
Most preferred way is with actual server
Gunicorn or something similar
Hi guys I have a problem with total count of items using django framework
In the html Iโm using the tag count to get the total number of the items but it depends on the paginator size
If my page is paginated to contain 5 elements and in the db there are 10 elements the tag in the html will give me 5 elements
What should I do to get the total of 10 even the page is paginated
By the way Iโm using class based views
I am using Vagrant in Windows 10.
I am in Vagrant ssh and have activate virtual environment for Django app
Now, I want to setup pycharm to use that virtual environment as wel..
In here, I am not able to figure out the path /home/vagrant/virtualenvs/project/bin/python3
Suppose I get this path in vagrant ssh, then where is this located in My Windows, so that I can use it for pycharm virtual env activation.
Below is the accepted answer.
Snippet of Answer:
That worked nicely. I can even use the PyCharm interface to install packages into the virtualenv on the Vagrant VM.```
this line, in nginx.conf: include /etc/nginx/mime.types;, which grabs the mime.types file, what is it for? is it to add the proper HTTP header every time nginx has to respond a request?
Is there any magic that needs to happen on the Flask server side to getting Stripe's checkout embed working? I copy pasted the script and checkout button, but the button doesn't take me to the checkout
is it possible to mask a server ip address with a domain, while also not disabling the mobile site of the web page?
because when i mask the ip, and access it on my phone, it shows the desktop version
I don't know about the issue, but it sounds like something related to the proxy server configuration
hello
I was using pythonanywhere to get started
but they limit chronjobs to once per hour for paid users
i need to do once every 2 minutes
is there a better site?
well, you probably can make use of AWS free tier for a while
not for free
i don't mind paying
I was going to pay for pythonanywhere to get mogodb access
but then I realised i would be wasting money
if I can only do 20 chronjobs a day
Could you not just rent a vps from ovh? It's only like 3ยฃ/mnoth
The config you need to do might be annoying, but it's a 1 time thing and useful to know
that has a public accessible web interface? python and connection to mongodb?
lots of people
lots of companies use Django and other python web frameworks
lots of Applications use Django REST
Instagram uses Django
@vagrant adder Why wouldn't they use HTML or Java or JavaScript to create websites what does python bring that is exclusive. Java script makes websites pretty and HTML actually makes the website. What is python use that is so valuable.
Well, python is used for backend
Data bases with libraries ?
Html,css and js is used for things you actually see
Yes, databases and data management
oh okay
what do you use python for. Storing encrypted passwords, user personal info etc?
okay
Do you use an FTP when your using python like using it in a plugins folder or something?
@vagrant adder
No I mean like when creating a website do you use an FTP to access server files and use python plugins
interesting
That way i can fiddle with server
I only use .jar files when I work on servers
In my internship company i used to had physical access to the server
Like a big cupboard
Lel
Anyone good w/ serialization in django know if it's possible to serialize a model to JSON and descend to foreign keys as well? So if I have a model with a FK to another model, serializing it w/ the django core serializer just serializes the foreign key to its primary key. Instead I'd like to descend and serialize all that model's fields also.
Anyone around willing to briefly discuss some json/data structure info?
Quick question
Whats the best solution for plotting > 80k points on a map and dynamically changing points in real time?
@brave otter try mapping it to a dictionary and then using the json.dumps function
@nova tapir
Sure
For production what are the alternatives to flask
Something that is able to handle huge amount of load
I know one is Sanic
anything apart from this too
that would be efficient af.
django?
also, flask is totally okay for production
but with dedicated webserver
like gunicorn
how do i know whether to use flask or django or electron?
what even is electron can i combine electron with django or something - im new to webdev
Electron is a way to make desktop apps using JavaScript. It can be combined with Django/Flask if you design your backend Django/Flask as a REST based service
Choosing between Flask or Django depends on quite a lot of different things - Electron doesn't really compete with either and is far more likely to be used in conjunction with either
does node.js compete with django and flask
not node, but express.js(web framework)
@mortal fern you can look at boostrap css https://getbootstrap.com/
@surreal thicket Thanks, looks like this is what I was looking for. seems I can just use a link to load BootStrap's CSS, then anything I wanna modify/override I can just load my own CSS touching only what I wanna change after Bootstrap
@vagrant adder How do I choose a framkework
I only want to create a scalable API so
should I go with flask
or tornado
i have never used tornado
for async, sanic and tornado are great choices
If you just want a simple and lightweight framework like flask, go with sanic - it is light and pretty fast
do you have any idea
with sanic
its pretty new
so I would not be able to find resources if I get stuck
@balmy forge
Yes I did
It's a favourite of many, it's almost identical to flask
And it's pretty fast
If you care about performance, consider giving this a read https://blog.startmatter.com/top-fast-python-web-frameworks-in-2019/
It's not used by many
okay
With flask-marshmellow + sqlalchemy, how do I build a proper schema that hides a password?
I have 3 fields for a user model that look like: py id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(255)) password = db.Column(db.String) And I would like to hide the password when returning a schema
@steel tiger define โhiding a passwordโ
Currently it's just this py class UserSchema(ma.ModelSchema): class Meta: model = User
Remove the password field from returning from the schema
Because this isn't great
New to marshmallow, first time using it
First, never store a plain password in your database, always salt it and hash it (use PBKDF2 https://en.wikipedia.org/wiki/PBKDF2)
Then, simply define two schemas, one for creating an user, one for retrieving/displaying an user
Already done
What do you think the hashed password is there for :P
(Using bcrypt)
What would a simple schema look like? Can't figure out the methodology of schemas
So you'll have
from marshmallow import fields, Schema
class CreateUserSchema(Schema):
username = fields.Str()
password = fields.Str()
class UserSchema(Schema):
id = fields.Integer()
username = fields.Str()
You'll use the first one to create an user (in user registration view for instance), the second one to display it (when an user retrieves its information for instance)
Using flask_marshmallow fyi, will it be the same with fields?
They'll probably be similar, I do not know the details of flask_marshmallow, but it would not be very different imo
The concept you have to grasp is that schemas are โa way to represent dataโ, you can (and often should) use multiple schemas for the same data model
Yeah
Ahh I see
class UserSchema(ma.Schema):
class Meta:
fields = ("username",) ```
That's neato
๐
Wow, using webargs + marshmallow is really nice
class UserApi(Resource):
args_id = {"id": fields.Int(required=True)}
args_make = {
"username": fields.Str(required=True),
"password": fields.Str(required=True),
}
@use_kwargs(args_id)
def get(self, id):
user = UserModel.query.get(id)
if user is None:
return {"status": "not found"}, 404
return {"status": "success", "data": user_schema.dump(user)}, 200
@use_kwargs(args_make)
def post(self, username, password):
new_user = UserModel(username=username, password=password)
db.session.add(new_user)
db.session.commit()
return {"status": "success"}, 200
So compact and readable
Oh geez, i'll have to implament authlib
Or I could just cheat and use the api alongside the webapp
gonna try asking about this again; I'm trying to debug some issues in a setup where a server running django / uWSGI sends tasks to another VPS running the worker processes; recently upgraded Django 1.9 -> 1.11 and Celery (something, i can look it up if it matters) -> 4.3, and it introduced a lot of errors that appear to reside in the communication between the web and worker VPSes
more specifically i'm not 100% sure but it does appear that uwsgi processes which call task.delay() or similar, are getting stuck waiting for some kind of response from the worker which either does not come, or comes too late.
all of this is running on python 2.7; of course we are trying to migrate to python3/django2 by the end of the year, but, y'know. only a few steps at a time.
@gleaming herald Starlette is currently the fastest async python web framework. its creators also created the ASGI spec it runs on, which other things are starting to implement as well, including django. so not bad to familiarize yourself with it
is ASGI a significant improvement over WSGI?
every time i hear "fastest" i think "with what caveats"
To me it wasn't a difference of speed it was rather "this can be async and this other thing can't"
Well there's probably some way to use WSGI asynchronously but I haven't seen anything
what does sanic do?
It uses ASGI I believe
Ok well that's not entirely accurate
It can use ASGI, but also has its own webserver and gunicorn support
@surreal thicket ASGI is essentially the successor to WSGI. you've already left WSGI in the dust if you're using any sort of true async web framework/server and do things like run websockets properly. it's just that until recently, all the packages that were able to do this were doing it their own way
ASGI is an actual spec and everything that uses it can be bolted onto anything else that uses it with a one-line command. that aside, the web framework it powers (Starlette) just happens to outperform the others
there's nothing wrong with sanic and in many ways it is nicer to work with than starlette and more mature
aiohttp is a step down from there in that it's even more mature but also among the slowest of the async frameworks while also not being quite as easy as sanic to work with most would say
but theyre all fine if one just works with your brain better than the others. provided the performance difference isn't something you are concerned about. something like tornado, though, is something you should probably try to forget exists
I actually found aiohttp pretty fast, benchmarks between aiohttp and tornado and sanic yields amazing results in aiohttp leading, but simply because you will need to implement everything again with aiohttp
i just also like starlette because though the newest it is currently moving incredibly fast, and since they wrote the underlying spec and a bunch of other tools i also use, the interaction between them (and eventually even things like django) is pretty great
aiohttp is definitely not slow. it's nice to use, the most mature, has been around a while, far better than using not async, etc
Oh wow, I did not know about this, many thanks for sharing
but if you are building something where squeezing every drop of performance out of your app is a potential big deal for costs and whatnot, starlette obliterates it
(for many practical uses)
1 and 2 on that list are starlette and uvicorn. which are both made by the same people, uvicorn being the web server that powers starlette
Oh no I completely understand and agree on that
I dont see sanic here though
tornado being slower than flask is a surprise
nobody may have run sanic tests for this site lately but i promise you it's faster than most but not faster than starlette. but you should use whatever does what you need and you feel comfortable with
sanic can do a lot and has the flask-esque extnesion community
I've used sanic and benchmarked with it before, so I know how fast it is
aiohttp doesnt has nearly as much of that, or anything baked in. starlette has at least the interfaces and mechanisms for you to use for things like auth, app config, graphql, background tasks/queues, etc, stuff you may have to bolt on yourself in other places
if you're building a very structured REST API, you ought to look at this: https://fastapi.tiangolo.com
FastAPI framework, high performance, easy to learn, fast to code, ready for production
Most interesting
it's built on top of starlette, but generates for you everything you need for a type-enforced REST API including the swagger pages and tests and such
Why didnt I talk to you before I build my backend lol
(the guys who made starlette are also respondible for apistar, which does the autogenerating swagger/openapi stuff, and are also the guys who made and maintain django-rest-framework which they based it on)
everyone has different needs, and ive only had my own experience
but part of that experience was going all in on async very early on and having built very high-traffic things using all of these frameworks and shooting myself in the foot plenty of times
the move from aiohttp to starlette for our main backend service that lots of users remain connected to via websockets and most clients are pushing messages to pretty frequently... the move gave about a 3-4x increase just instantly of how many concurrent ws connections it could reasonably manage on the same server specs with the same or better response times
for the types of apps we're building, that kind of stat is not insignificant in our survival because it's the heaviest service and is now servicing more users with lower costs
but i know that's not everyone's use case, or most peoples', and sticking to any of these async, non-tornado options that works best for you isnt a bad idea
@junior cloak wanna say thanks for telling me about fastapi, it's really fantastic and a pleasant to work with. It does fit what I need perfectly for a python - react project
@balmy forge haha perfect! everyone has different needs and no one thing is best for everyone, but there's some great stuff out there and it's nice when you find the right one
fastapi looks great.. im going to bookmark that page and get back to it
i'm working heavily with graphql now so fastapi isn't something i'm using much, but man i would've been all over that 3ish years ago doing all that work manually. hell of a project though and it did convince me to start using pydantic for a ton of things and dive into the type annotation world more than i had been
there should really be a better way to find some of these things. like if awesome-python (or any of the "awesome" lists) were not just kept up to date but if you could just click like a few tags that are your top priorities or use-cases and have it spit out some options that are best with that combination
i was looking at specs earlier for a piece of video editing software, and instead of just the usual "system requirements" that most software has, it was a very large table, where the primary field was the sort of video work you'll be doing, another field was the format of your source material, and the next fields were the recommended computer specs to best service that exact combo. thought that was pretty smart
anyways, stray thought. enjoy the cool web frameworks 2019 has given us
I would like to delete a user but when I do, i'd like to delete all of it's relationships. How do I do this?
(As in a user has 100 posts, how would I delete them posts cleanly if the user purges their account)
Also, how do you send a file (zip specifically) into a flask-restful api. Using webargs
@steel tiger i'm not sure deleting is necessarily the right thing to do to a "post" when a user is deleted
if these posts are part of conversation threads, just deleting can make it unclear what people are replying to
you may want to delete the text and/or the association to the username for privacy purposes, but that isn't necessarily possible to do as a simple sql constraint
So I have to loop through and "delete" them with a custom helper func?
Thinking how discord has it
with a well-defined data schema you could probably do it as a sequence of sql statements within a transaction
no looping necessary (the way discord does it is extremely inefficient because it's not an intended feature beyond the ability to delete individual posts or clear whole channels)
I could loop through the users posts and call a helper func contained in the post model
you really shouldn't need a loop
whatever you decide for the design, you're doing the same thing to every post, it can almost certainly be an update with a where clause
Oh I'm using sqlalchemy
flask-sqlalchemy even
Where it's not favoured to use pure sql
Not sure if this is a thing py from sqlalchemy.sql.expression import bindparam stmt = addresses.update().\ where(addresses.c.id == bindparam('_id')).\ values({ 'user_id': bindparam('user_id'), 'email_address': bindparam('email_address'), })
er that isn't really the same scenario, but sqlalchemy apparently does have facilities for updates with where-criteria instead of single model object updates
Yeah
You don't really have them in flask-sqlalchemy
It's more x number of model classes and you do it all by changing an object from the model class then commiting it
I really doubt that
I'll see if there is an equivilant
that might be the only way you know how to do it, but here's someone else showing another way to do bulk updates, this time specifically for flask sqlalchemy (though I can't find any clear indication that "flask-sqlalchemy" wouldn't give you all of sqlalchemy anyway) https://stackoverflow.com/questions/6699360/flask-sqlalchemy-update-a-rows-information
by using query.filter_by(...).update(...)
Yeah I know that
Just thinking in terms of bulk
I have devised a scheme in my head to do it but i'd have to loop over all models in a relationship
that probably isn't necessary and is going to be a performance bottleneck if you have a large number of records to update
i'm almost certain sqlalchemy/flask-sqlalchemy provides you the tools to make the DB execute a single update statement that updates them all - an ORM that couldn't would be pretty worthless
Yeah
May have found my saviour
Oh
Just a simple hidden pattern..
I guess that could work
Just render as deleted user
And blank account info
keep in mind also that you may have GDPR obligations or similar with regards to storage of data a user intends to delete
i'm not a lawyer though
hey, can someone help me find websockets of fb live video for a project, pls ping me if u know anything about it
Anyone have any experience with a Flask/React stack?
If so, what is the easiest way to pass an auth token between containers? Passing it down the tree, Context API, or shall I just accept that I need to use Redux?
ask @normal karma he works with flask and react
Yoooo
I have built a flask app in one file but its like 600 lines and I want to split it into multiple files
Does anyone have a good strategy/way/tutorial to do this?
appreciated.
take a look into the mvc pattern
https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
This will probably necessitate rebuilding your app from the beginning, but this was a pretty good tutorial
@rigid laurel Depends on your component structure, and where you need to access your token. If you want to access the token in only one component, fetch it and put it in the component's state (or state hook). If you need to access it in multiple components, you have a few options: find a common parent component (you can do it in root App component), to avoid callback madness, and just pass the token to child components via props. That can be really tedious, so I'll advise you to use Redux. I've, personally, was avoiding to use Redux because I just didn't understand it at first, but I picked it up very fast. Also, it synthesizes with Functional Components really well if thats your thing. I strongly suggest you take a look at Redux, it will make your life a lot easier.
Its a long response, take your time with it
I think you're almost certainly right and I guess I'll get started with Redux. Thanks for the detailed response
Glad I could help :)
hey everyone
I'm trying to figure out how to sort an html table based on some nested data structures, for example:
results = {
"A": {
"is_data": [
{key1: val1, key2: val2,}
],
"bs_data": [
{key1: val1, key2: val2,}
],
...
},
"B": {
"is_data": [
{key1: val1, key2: val2,}
],
"bs_data": [
{key1: val1, key2: val2,}
],
...
},
...
}
I'd like to be able to sort A and B based on things like val1 and val2.
for now each of the *_data items will only have one object in it
my goal is to have A and B reorder themselves based on some of the values in the sub-objects. So if A['is_data'][0]['key2'] > B['is_data'][0]['key2'] I'd like them in this order, but if not I'd like them flipped.
any thoughts? I'm working on it while I wait
This might be easier to visualize...I want to sort the keys of results based on things like calendardate, datekey, reportperiod, etc
Depends how large it is