File tree Expand file tree Collapse file tree 5 files changed +37
-63
lines changed
6 Web Page with Flask/basic page Expand file tree Collapse file tree 5 files changed +37
-63
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 11'''
2- TO use this code, you will first need to install the three packages being imported below using pip or a manual install method.
2+ To use this code, you will first need to install the three packages being imported below using pip or a manual install method.
3+ This code was updated in August 2021 to use the new KSU news feed design.
34'''
45from bs4 import BeautifulSoup
56import requests
Original file line number Diff line number Diff line change 1+ '''
2+ This code is a demo of how to create a database with a schema structure in Python using SQL Lite.
3+ For more information, see: https://datatofish.com/create-database-python-using-sqlite3/
4+ '''
15import sqlite3
2-
6+ #we can name the file antyhing we want, but it is common to use .db
37db_file = "new.db"
48
5- connection = sqlite3 .connect (db_file )
9+ #the connection is the database file itself represented in python
10+ connection = sqlite3 .connect (db_file )
11+
12+ #the cursor is the object that will allow us to execute SQL commands
13+ cursor = connection .cursor ()
14+
15+ #execute runs the SQL command but does not save it, the results are just in memory for now
16+ cursor .execute ('''
17+ CREATE TABLE IF NOT EXISTS products
18+ ([product_id] INTEGER PRIMARY KEY, [product_name] TEXT)
19+ ''' )
20+
21+ cursor .execute ('''
22+ CREATE TABLE IF NOT EXISTS prices
23+ ([product_id] INTEGER PRIMARY KEY, [price] INTEGER)
24+ ''' )
25+ #commit is when we actually save the changes we made to the database
26+ connection .commit ()
Original file line number Diff line number Diff line change 1+ '''
2+ This is a basic web page running with Flask.
3+ '''
4+
15from flask import Flask , render_template , request
6+ #This is the Flask object, we are creating an instance of the Flask class and storing it in the variable app
7+ #__name__ is a special variable in Python that is the name of the module, which is this file's name without the .py extension
28app = Flask (__name__ )
9+
10+ #This is a decorator, it is a function that takes a function as a parameter!
11+ #A decorator is a function that wraps another function
12+ #This decorator is saying that when someone goes to the URL /greet, run the greet function
313@app .route ('/greet' , methods = ['POST' ])
414def greet ():
515 inputName = request .form ['myName' ]
616 ip = request .remote_addr
717 #write data to file or to DB
818 inputName = inputName .upper ()+ " hi! Visiting from " + str (ip )
919 return render_template ("home.html" ,myName = inputName )
20+
1021@app .route ('/' )
11- def home ():
12-
22+ def home ():
1323 return render_template ("home.html" ,myName = "Type your name in the box and click submit!" )
1424
1525@app .route ('/about/' )
You can’t perform that action at this time.
0 commit comments