WELCOME TO

Python Workshop Day 2

Learn how to build Python web applications using Streamlit, connect SQLite database and develop real-time mini projects using Python backend logic.

Start Learning

What is Streamlit?

Python UI Development Framework

Streamlit is a Python library used to build web applications easily using Python code.

It helps developers create attractive UI applications without HTML or JavaScript.

Install Streamlit

pip install streamlit

Run Streamlit Application

streamlit run app.py

Student Exercises

  • Install Streamlit in your system.
  • Create a file named app.py.

First Streamlit Application

Create Your First UI Application

Streamlit provides simple functions to create headings, messages and UI components.

import streamlit as st

st.title("My First App")

st.write("Welcome Students")

Student Exercises

  • Create a heading using your name.
  • Display a welcome message using st.write().

User Input in Streamlit

Read User Data

Streamlit provides input components to read data from users.

Text Input

name = st.text_input("Enter Name")

st.write(name)

Number Input

marks = st.number_input("Enter Marks")

st.write(marks)

Student Exercises

  • Read employee name using text_input().
  • Read employee salary using number_input().

Button Operations

Perform Actions Using Buttons

Buttons are used to perform actions when users click them.

if st.button("Save"):

    st.success("Button Clicked")

Student Exercises

  • Create a Submit button.
  • Display success message after button click.

SQLite Database

Database Connectivity Using Python

SQLite is a lightweight database available directly in Python.

It helps store and retrieve data from applications.

Database Connection

import sqlite3

con = sqlite3.connect("student.db")

cursor = con.cursor()

Create Table

cursor.execute(
"CREATE TABLE IF NOT EXISTS student(
id INTEGER PRIMARY KEY,
name TEXT
)"
)

con.commit()

Insert Data

cursor.execute(
"insert into student(name) values(?)",
("Ravi",)
)

con.commit()

Fetch Data

cursor.execute("select * from student")

data = cursor.fetchall()

print(data)

Student Exercises

  • Create employee table in SQLite database.
  • Insert employee name into database.

Mini Project

Student Marks Management System

This application accepts student marks, calculates total marks and stores data into SQLite database.

import streamlit as st
import sqlite3

con = sqlite3.connect("student.db")

cursor = con.cursor()

cursor.execute(
"CREATE TABLE IF NOT EXISTS student_marks(
id integer primary key autoincrement,
name TEXT,
marks INTEGER
)"
)

con.commit()

st.title("Student Marks Management System")

name = st.text_input("Enter Student Name")

s1 = st.number_input("Enter Subject 1 Marks")

s2 = st.number_input("Enter Subject 2 Marks")

s3 = st.number_input("Enter Subject 3 Marks")

total_marks = s1 + s2 + s3

if st.button("Save Data"):

    cursor.execute(
    "insert into student_marks(name, marks) values(?,?)",
    (name, total_marks)
    )

    con.commit()

    st.success("Data Stored Successfully")

if st.button("View Data"):

    cursor.execute("select * from student_marks")

    data = cursor.fetchall()

    st.dataframe(data)

con.close()

Student Exercises

  • Modify the application for 5 subjects.
  • Add average calculation to the project.