135201
You will be muted until you read the rules. Read them! @Rules_for_Python A group about the Python programming language. Offtopic things go here: @pythonofftopic Resources to learn python: @pythonres Group for Hy: @hylang Selenium: @SeleniumPython
Hello, I want to become a data analyst. Is there anyone who can help me?
Читать полностью…
You may want to read up on
https://www.freecodecamp.org/news/what-is-a-floating-point-arithmetic-problem/
Then after reading that article, read this one
https://realpython.com/ref/stdlib/decimal/
honestly at this point just pick flask-sqlalchemy and stop overthinking the DI stuff, youre going in circles 😭 but if you really want the yield pattern it does work the same
Читать полностью…
What is that got to do with using the library?
And why do you keep existing on using DI?
You dont need it, you can just use with Session(engine) each time and be done with it
Anyone Who useed sqlmodel and sqlite like this thigns can say me how this happens?
the docs say thsi exactly, but how i can apply this the sqlmodel docs not say this can anyone know how to do this in real production code,
Some databases don't support foreign key constraints.Читать полностью…
For example, SQLite doesn't support them by default. They have to be manually enabled with a custom SQL command:
PRAGMA foreign_keys = ON;
So, in general it's a good idea to have both cascade_delete and ondelete configured.
This is not a Python specific question, please delete your offtopic message, move to @PythonOfftopic and ask there.
Читать полностью…
Look, you dont need to use this DI or anything
But i think you should start with flask-sqlalchemy as its much easyer to find tutorials on this combination, then when you learn how sqlalchemy works (which SQLModel is based on) it will be easyer to adapt SQLModel to Flask
actually i will use Sqlmodel, and sqlmodel docs includes the fastapi section, so it got little problamatic, one thing if i will use yield to generate the session and use this in my database function, then when does this with block ends in my routes or in my database function, so how it knows when to finish
flask-di> Depends
knows when to finish this with block!
as fastapi docs say after ends of one request the session got removed automatically.
docs:-
If it had yield, then it will continue the rest of the execution once you are done sending the response. In the case of the session, it will finish the cleanup code from the with block, closing the session, etc.And because dependencies can use yield, FastAPI will make sure to run the code after the yield once it is done, including all the cleanup code at the end of the with block. So we are also fine with that. ✅Читать полностью…
The with Block
Yes, the usecase is the same, it does not care how you get the object or what that injected object is.
In the end, you inject into the function whatever you want, it can be anything you want.
Examples on FastAPI site are meant for beginners, separating routing from your models and controllers is the usual pattern in real life called MVC (Model-View-Controller)
Its a very simple thing that adds request-based dependancy injection
There is like 5 things it does, so i dont feel like spinning up the webpage just to show what is in the readme.
Its very simple peace of code (~130 lines of code) and something i use personally on my flask apps.
Readme with a few examples is really everything you need to use it.
As a beginner, i would advise you to not force yourself onto these specific design patterns, but to build your project in the most crude and simplest manner
Then when you are happy with how it WORKS, then you can refactor it and make it more "pythonic" or whatever
Just read the description in readme.
It provide Depend() like functionality for flask apps.
You won't use FastAPI one, that's a completely different framework.
My wrapper library just provides dependency injection in Depends() style.
You should maybe learn about dependency injection first?
https://dev.to/hongster85/dependency-injection-understand-in-3-minutes-3a5k
And you can't really use this response_model=whatever in flask.
That's FastAPI concept, not flask's
So build it this group is not for request ready made bots
Читать полностью…
from ur github deployment it shows https://pypi.org/project/flask-di/
but it not opens there
I Recently see one things when reading the sqlmodel docs, see there it say,
like, 1.1 + 2.2 will be not equals to 3.3 but it will near of 3.3 like,
price = 1.1
tax = 2.2
total = price + tax
print(total)
—->
3.3000000000000003
but ur Depends on the Flask already application looks so beautiful and most easy like sqlmodel docs say this...
Читать полностью…
Or even better, use flask-sqlalchemy and use any tutorial
Читать полностью…
i tried to learn and watch the library you made so that i can use this in my application of flask,
see i found there,
https://github.com/razorblade23/di-flask/blob/master/tests/test_basic.py
here how i will do test? i cloned and did uv sync already.
but how the test i will perform on this repo you wrote here.
That is the point of DI
Bur you are clearly a beginner, so quit making stuff up
Learn flask with flask-sqlalchemy
i found by reaction that most of here say to learn using flask-sqlalchemy, but i did some docs checking here, and thinks when i can do this easily with less code and clean code in di like concept of fastapi with sqlmodel + flask so so will not it be more problamatic or waste of time to learn and use flask-sqlalchemy as here is also session like concept not properly understandable.
Читать полностью…
Hey Guys,
Do You think, using Flask+ Sqlmodel is ok or
flask + flask-sqlalchemy will more better choice,
as i am confusd that sqlmodel to use i need to use session like concept and need di for writing real db codes, or with flask-sqlalchey with flask.g and find seassion will be more better, i am confusd what looks more realistic for real production?
And you are working with Flask
I think looking at FastAPI docs only confuses you.
see in sqlmodel > fastapi docs it shows the database function in the routes level code.
But see in my flask routes.py when i my fun execute for a endpoint like
get /heros
a function is executed, and this fun call another database related function, 'get_all_heroes' , traditionay i have a with session block in that db function, but as u say to use Depends from ur library, so in this case i will use
def get_all_heroes(*,session: Session = Depends(get_session),)
...
def get_session():
with Session(engine) as session:
yield session
yes i will try to learn and understand the di more better way, then maybe read and use the ur library this,
why don't you have not any documentation page for this library, is this because ur library is just use in one fun with Depends only this is the work of this library?
previously i didnot found this sorry, as i didnot see this pypi link in ur readme, i thought this is not exists in pypi, now i get this,
https://pypi.org/project/di-flask/
This i found, maybe i need to learn a little more to use ur library.
So ur library will help me to use the session: Session = Depends(get_session)
in my flask routes function so that i dont need to write the with block in all the database related function, am i right?
but can you say how i will use the Depends() <- fastapi, as this comes from fastapi here how i will use this in my flask routes.py, ooo ur library has this one Depends 💚
Flask does not have Depends function, FastAPI does
What do you mean there is no PyPi project for di-flask?
https://pypistats.org/packages/di-flask
Documentation is in README in GitHub repository
https://github.com/razorblade23/di-flask
Its very simply to use and basically gives you that Depends that FastAPI has but for the use in flask
As Per Sqlmodel Docs below is the sample code:
def get_session():
with Session(engine) as session:
yield session
# Code here omitted 👈
@app.post("/heroes/", response_model=HeroPublic)
def create_hero(*, session: Session = Depends(get_session), hero: HeroCreate):
db_hero = Hero.model_validate(hero)
session.add(db_hero)
session.commit()
session.refresh(db_hero)
return db_hero
session: Session = Depends(get_session)