Bind element contents to database updates #4413
Unanswered
daniellivingston
asked this question in
Q&A
Replies: 1 comment 5 replies
-
If you don't mind me asking, how are you updating the database with the input function? from nicegui import ui, app
from sqlmodel import Session
from models.main import engine, User
class MainPage:
def __init__(self, session):
self.session = session
self.user = self.session.query(User).first()
self.render()
def render(self):
self.label = ui.label("Hello user! " + self.user.name)
#when the user presses enter, we want to change the username
self.inputter = ui.input("Enter your (new) name").on("keydown.enter", self.change_username)
def change_username(self, event):
# update the model
self.user.name = self.inputter.value
# update the database
self.session.commit()
self.label.text = "Hello user! " + self.inputter.value
self.label.update()
@ui.page("/")
def index():
# get the first user in the database
with Session(engine) as session:
MainPage(session)
ui.run(port=8080, host="0.0.0.0") You'll need to modify it to your use-case. Let me know if you have an MRE you could give, or if you need more help from there |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Question
Hi, I am using SQLAlchemy with an SQLite backend. I want to be able to update the contents of an element, like a
ui.label
's text, under certain database updates.For example, say you have a page like this:
Normally, you might use something like
on_change
orbind_value
to link theui.label
with theui.input
.In my case,
ui.input
would update the "name" column of the "profile" SQLite database on row Xui.label
's text is updated automatically from the correct row / column of the SQLite "profile" databaseWhat can I do to solve (2)?
Beta Was this translation helpful? Give feedback.
All reactions