인공지능 수업(Python)
인공지능 6/22
TalleQQ
2021. 6. 22. 17:38
#파이썬
#Python
#인공지능
- 입력했을 때 결과창 나오기
app.py
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def student():
return render_template('student.html')
@app.route("/result",methods=['POST',"GET"])
def result():
if request.method == 'POST':
result = request.form
return render_template("result.html", result=result)
if __name__ == '__main__':
app.run(debug=True)
student.html
<html>
<body>
<form action="http://localhost:5000/result" method="POST">
<p>Name <input type="text" name="Name" /></p>
<p>Physics <input type="text" name="Physics" /></p>
<p>Chemistry <input type="text" name="chemistry" /></p>
<p>Maths <input type="text" name="Mathematics" /></p>
<p> <input type="submit" value="submit" /></p>
</form>
</body>
</html>
result.html
<!doctype html>
<html>
<body>
<table border=1>
{% for key, value in result.items() %}
<tr>
<th> {{ key }} </th>
<td> {{ value }} </td>
</tr>
{% endfor %}
</table>
</body>
</html>
----------------
> home.html <
----------------
<html>
<head>
<title>home</title>
</head>
<body>
<h3>Welcome to the website</h3>
<a href="/list">학생 목록</a><br>
<a href="/enternew">학생 추가</a><br>
</body>
</html>
-------------------
> student.html <
-------------------
<html>
<body>
<form action="{{ url_for('addrec') }}" method="POST">
<h3>Student Information</h3>
Name<br><input type="text" name="nm" /></br>
Address<br><textarea name="add"></textarea><br>
City<br><input type="text" name="city" /><br>
PINCODE<br><input type="text" name="pin" /><br>
<input type="submit" value="submit" /><br>
</form>
</body>
</html>
----------------
> list.html <
----------------
<!doctype html>
<html>
<body>
<table border=1>
<thead>
<td>Name</td>
<td>Address</td>
<td>city</td>
<td>Pincode</td>
</thead>
{% for row in rows %}
<tr>
<td>{{row["name"]}}</td>
<td>{{row["addr"]}}</td>
<td>{{row["city"]}}</td>
<td>{{row['pin']}}</td>
</tr>
{% endfor %}
</table>
<a href="/">Go back to home page</a>
</body>
</html>
------------------
> result.html <
------------------
<!doctype html>
<html>
<body>
result of addition : {{ msg }}
<h2><a href="\">go back to home page</a></h2>
</body>
</html>
---------------
> app.py <
---------------
import sqlite3 as sql
from flask import Flask, render_template, request
conn = sql.connect('database.db')
print("Opened database successfully")
conn.execute(
'CREATE TABLE IF NOT EXISTS students (name TEXT, addr TEXT, city TEXT, pin TEXT)')
print("Table created successfully")
conn.close()
''''''
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/list')
def list():
con = sql.connect("database.db")
con.row_factory = sql.Row
cur = con.cursor()
cur.execute("select * from students")
rows = cur.fetchall()
return render_template("list.html", rows=rows)
@app.route('/enternew')
def new_student():
return render_template('student.html')
@app.route('/addrec', methods=['POST', 'GET'])
def addrec():
if request.method == 'POST':
try:
name = request.form['nm']
addr = request.form['add']
city = request.form['city']
pin = request.form['pin']
with sql.connect("database.db") as con:
cur = con.cursor()
cur.execute("""INSERT INTO students(name, addr, city, pin)
VALUES(?, ?, ?, ?)""", (name, addr, city, pin))
con.commit()
msg = "Record successfully added"
except:
con.rollback()
msg = "error in insert operation"
finally:
return render_template("result.html", msg=msg)
con.close()
if __name__ == '__main__':
app.run(debug=True)