from flask import Flask
app = Flask(__name__)
@app.route('/')
def index() -> str:
return '<p>Hello Flask!</p>'
if __name__ == '__main__':
app.run(debug=True)
from flask import Flask
app = Flask(__name__)
@app.route('/')
def index():
greet_with_link = """<h1>Hola, mundo!</h1>
<p><a href="user/Anikin/Georgy">Presióname</a></p>"""
return greet_with_link
@app.route('/user/<surname>/<name>')
def get_user(name, surname):
personal_instruc = f"""<h1>Hola, {surname} {name}!</h1>
<p>Cambia el nombre de usuario en la barra de dirección y vuelve a cargar la página</p>"""
return personal_instruc
if __name__ == '__main__':
app.run(debug=True)
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/about")
def get_page_about():
return render_template("about.html")
if __name__ == "__main__":
app.run(debug=True)
<!DOCTYPE html>
<html>
<head>
<title>Main page</title>
</head>
<body>
<h1>Página principal</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>About</title>
</head>
<body>
<h1>Acerca de la app</h1>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}Aplicación Flask{% endblock %}</title>
</head>
<body>
<h1>{{h1}}</h1>
</body>
</html>
about.html:
{% extends 'base.html' %}
{% block title %}About{% endblock %}
index.html:
{% extends 'base.html' %}
{% block title %}Main page{% endblock %}
about.html:
{% extends 'base.html' %}
{% block title %}About{% endblock %}
index.html:
{% extends 'base.html' %}
{% block title %}Main page{% endblock %}
<!doctype html>
<html lang="es">
<head>
{% block head %}
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
{% block styles %}
<!-- Bootstrap CSS -->
{{ bootstrap.load_css() }}
{% endblock %}
<title>{% block title %}Aplicación Flask{% endblock %}</title>
{% endblock %}
</head>
<body>
<!-- Contenido de tu página -->
{% block content %}
<div class="jumbotron text-center">
<h1>{{ h1 }}</h1>
</div>
{% endblock %}
{% block scripts %}
<!-- JavaScript opcional -->
{{ bootstrap.load_js() }}
{% endblock %}
</body>
</html>
{% extends 'bootstrap/base.html' %}
from flask_bootstrap import Bootstrap4
from flask import Flask, render_template
app = Flask(__name__)
bootstrap = Bootstrap4(app)
@app.route("/")
def index():
return render_template("index.html", h1 = "Página principal")
@app.route("/about")
def get_page_about():
return render_template("about.html", h1 = "Acerca de la app")
if __name__ == "__main__":
app.run(debug=True)
{% extends 'bootstrap/base.html' %}
{% block title %}Main page{% endblock %}
{% block content %}
{{super()}}
<div class="container text-center">
<form class="d-inline-block" style="max-width: 33%;">
<div class="form-group">
<label for="eventDate">Fecha</label>
<input type="date" name="eventDate" class="form-control" placeholder="Fecha события">
</div>
<div class="form-group">
<label for="eventName">Evento</label>
<input type="text" name="eventName" class="form-control" placeholder="Nombre события">
</div>
<div class="form-group">
<label for="eventDuration">Duración</label>
<input type="number" name="eventDuration" class="form-control" placeholder="Продолжительность" min="1" max="24">
</div>
<button type="submit" class="btn btn-primary">Enviar</button>
</form>
</div>
{% endblock %}
from datetime import datetime
from flask import Flask, redirect, render_template, request
from flask_bootstrap import Bootstrap4
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
bootstrap = Bootstrap4(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///events.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Event(db.Model):
id = db.Column(db.Integer, primary_key=True)
date = db.Column(db.Date, nullable=False)
name = db.Column(db.String(255), nullable=False)
duration = db.Column(db.Integer, nullable=False)
def __str__(self):
return (
f"Nombre: {self.name}\n"
f"Fecha: {self.date}\n"
f"Duración {self.duration}ч"
)
@app.route('/', methods=['POST'])
def add_event():
date = datetime.strptime(request.form['eventDate'], '%Y-%m-%d').date()
name = request.form['eventName']
duration = int(request.form['eventDuration'])
print(date, name, duration, sep='\n')
event = Event(date=date, name=name, duration=duration)
db.session.add(event)
db.session.commit()
return redirect('/')
@app.route("/")
def index():
return render_template("index.html", h1 = "Página principal")
@app.route("/about")
def get_page_about():
return render_template("about.html", h1 = "Acerca de la app")
if __name__ == "__main__":
with app.app_context():
db.create_all()
app.run(debug=True)
{% extends 'bootstrap/base.html' %}
{% block title %}Events{% endblock %}
{% block content %}
{{super()}}
<div class="container text-center">
<a href="{{ url_for('index') }}"><h2>Agregar evento</h2></a>
</div>
<div class="mt-4">
<ul class="list-group">
{% for event in events %}
<li class="list-group-item">
<p>{{ event }}</p>
</li>
{% endfor %}
</ul>
</div>
{% endblock %}
@app.route("/events")
def view_events():
events = Event.query.order_by(Event.date).all()
return render_template("events.html", h1 = "Eventos", events=events)
<a href="{{ url_for('view_events') }}"><h2>Ver eventos</h2></a>