I hereby request for a help on the app I am developing using Shiny for Python, I got stuck and I search ChatGPT and Bard AI I could not get the procedure of how to insert my data in the database using SQL statements; colleagues see the code below for the shiny app for python for your perusal
Many thanks for the support
import sqlite3
import re
from datetime import date
from pathlib import Path
from htmltools import div, tags, TagList
from shiny import *
from shinywidgets import output_widget, register_widget, render_widget
#-------------------path----------------------
current_dir = Path(__file__).parent if "__file__" in locals() else Path.cwd()
database_path = current_dir / "student.db"
# Create a database connection
conn = sqlite3.connect('student.db')
c = conn.cursor()
# Drop the existing table
c.execute('''DROP TABLE IF EXISTS std''')
conn.commit()
# Create a new table to store patient information
c.execute('''CREATE TABLE std
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
age INTEGER,
gender TEXT,
date DATE)''')
conn.commit()
#----------------------------Shiny UI------------------------------------------
app_ui = ui.page_fluid(
div(
ui.input_text("name", "Name"),
ui.input_numeric("age", "Age", value=0),
ui.input_select("gender","Gender",
choices=["Male", "Female"],),
ui.input_date("date", "Date Enroll"),
ui.input_action_button("submit", "Sumbit"),
style=
'width: 45%;'
"display: flex;"
'padding: 40px 10px;'
'margin: 8px 2;'
'display: inline-block;'
'border: 1px solid #ccc;'
'border-radius: 4px;'
'box-sizing: border-box;'
'border-radius: 30px;'
'background-color: #f69471;'
"height:95%;"
"gap: 0.6em;"
"flex-direction:column;"
"justify-content:space-around;"
"text-align:center;"
'transition: width 0.4s ease-in-out;'
'float:left;'
)
)
#------------------------server logic----------------------------------
def server(input, output, session):
@reactive.Effect
@reactive.event(input.submit)
def _():
cur = conn.cursor()
cur.execute('''INSERT INTO std (name, age, gender, date)
VALUES (?, ?, ?, ?)''',
(input.name,
input.age,
input.gender,
input.date,)
)
conn.commit()
app = App(app_ui, server)