Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 33 additions & 28 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
import os
from flask import Flask
from flaskext.mysql import MySQL # For newer versions of flask-mysql
# from flask.ext.mysql import MySQL # For older versions of flask-mysql
from flask import Flask, render_template
from flask_mysqldb import MySQL

app = Flask(__name__)

mysql = MySQL()
# Configuración de MySQL

mysql_database_host = 'MYSQL_DATABASE_HOST' in os.environ and os.environ['MYSQL_DATABASE_HOST'] or 'localhost'
app.config['MYSQL_HOST'] = os.environ.get('MYSQL_DATABASE_HOST', 'localhost')
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'root'
app.config['MYSQL_DB'] = 'mydatabase'

# MySQL configurations
app.config['MYSQL_DATABASE_USER'] = 'db_user'
app.config['MYSQL_DATABASE_PASSWORD'] = 'Passw0rd'
app.config['MYSQL_DATABASE_DB'] = 'employee_db'
app.config['MYSQL_DATABASE_HOST'] = mysql_database_host
mysql.init_app(app)
mysql = MySQL(app)

conn = mysql.connect()

cursor = conn.cursor()
#Rutas de la Aplicación

@app.route("/")
def main():
return "Welcome!"

@app.route('/how are you')
def hello():
return 'I am good, how about you?'
@app.route("/")
def home():
"""Página de inicio"""
return render_template("index.html") # Renderiza la plantilla HTML

@app.route('/read from database')
@app.route("/empleados")
def read():
cursor.execute("SELECT * FROM employees")
row = cursor.fetchone()
result = []
while row is not None:
result.append(row[0])
row = cursor.fetchone()
"""Consulta la base de datos y muestra los empleados en una tabla HTML"""
try:
conn = mysql.connection
cursor = conn.cursor()

cursor.execute("SELECT id, name FROM employees") # Asegúrate de que la tabla 'employees' existe
rows = cursor.fetchall()

cursor.close()
return render_template("empleados.html", employees=rows) # Enviar datos a la plantilla HTML

except Exception as e:
return render_template("error.html", error=str(e)) # Página de error si hay problemas



#Iniciar Servidor

return ",".join(result)

if __name__ == "__main__":
app.run()
app.run(debug=True)
Empty file added requirements.txt
Empty file.
27 changes: 27 additions & 0 deletions templates/empleados.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lista de Empleados</title>
<link rel="stylesheet" href="">
</head>
<body>
<div class="container">
<h1>Lista de Empleados</h1>
<table>
<tr>
<th>ID</th>
<th>Nombre</th>
</tr>
{% for empleado in employees %}
<tr>
<td>{{ empleado[0] }}</td>
<td>{{ empleado[1] }}</td>
</tr>
{% endfor %}
</table>
<a href="{{ url_for('home') }}" class="btn">Volver al Inicio</a>
</div>
</body>
</html>
16 changes: 16 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Aplicación WEB</title>
<link rel="stylesheet" href="">
</head>
<body>
<div class="container">
<h1>Bienvenido a Mi Aplicación Web</h1>
<p>Consulta la lista de empleados aquí:</p>
<a href="empleados.html" class="btn">Ver Empleados</a>
</div>
</body>
</html>