Commit 2f48bbf5 by Vitor Eloidio

Primeiro commit

parents
from flask import Flask, render_template, request, redirect
import sqlite3
app = Flask(__name__)
def conectar():
return sqlite3.connect("database.db")
# Criar tabela automaticamente
conn = conectar()
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS animais (
id INTEGER PRIMARY KEY AUTOINCREMENT,
brinco TEXT NOT NULL,
raca TEXT NOT NULL,
sexo TEXT NOT NULL,
peso REAL NOT NULL
)
""")
conn.commit()
conn.close()
@app.route("/")
def index():
conn = conectar()
animais = conn.execute(
"SELECT * FROM animais"
).fetchall()
conn.close()
return render_template(
"index.html",
animais=animais
)
@app.route("/cadastrar", methods=["GET", "POST"])
def cadastrar():
if request.method == "POST":
conn = conectar()
conn.execute(
"""
INSERT INTO animais
(brinco,raca,sexo,peso)
VALUES (?,?,?,?)
""",
(
request.form["brinco"],
request.form["raca"],
request.form["sexo"],
request.form["peso"]
)
)
conn.commit()
conn.close()
return redirect("/")
return render_template("cadastrar.html")
if __name__ == "__main__":
app.run(debug=True)
\ No newline at end of file
File added
Flask==3.1.3
\ No newline at end of file
body{
font-family: Arial, sans-serif;
background:#f4f4f4;
margin:0;
}
.container{
width:90%;
max-width:1000px;
margin:30px auto;
background:white;
padding:20px;
border-radius:10px;
}
h1{
color:#14532d;
}
.btn{
display:inline-block;
padding:10px 20px;
background:#14532d;
color:white;
text-decoration:none;
border-radius:5px;
margin-bottom:20px;
}
table{
width:100%;
border-collapse:collapse;
}
th,td{
border:1px solid #ddd;
padding:10px;
}
th{
background:#14532d;
color:white;
}
input,select{
width:100%;
padding:10px;
margin-bottom:10px;
}
button{
width:100%;
padding:10px;
background:#14532d;
color:white;
border:none;
}
\ No newline at end of file
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Cadastrar Animal</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="container">
<h1>🐂 Novo Animal</h1>
<form method="POST">
<input type="text" name="brinco" placeholder="Brinco" required>
<input type="text" name="raca" placeholder="Raça" required>
<select name="sexo">
<option>Macho</option>
<option>Fêmea</option>
</select>
<input type="number" step="0.1" name="peso" placeholder="Peso" required>
<button type="submit">
Salvar Animal
</button>
</form>
<br>
<a href="/">Voltar</a>
</div>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<title>Terra do Agro</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>
<div class="container">
<h1>🌱 Terra do Agro</h1>
<a class="btn" href="/cadastrar">
+ Cadastrar Animal
</a>
<table>
<tr>
<th>ID</th>
<th>Brinco</th>
<th>Raça</th>
<th>Sexo</th>
<th>Peso</th>
</tr>
{% for animal in animais %}
<tr>
<td>{{ animal[0] }}</td>
<td>{{ animal[1] }}</td>
<td>{{ animal[2] }}</td>
<td>{{ animal[3] }}</td>
<td>{{ animal[4] }} kg</td>
</tr>
{% endfor %}
</table>
</div>
</body>
</html>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment