Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Showing
with 346 additions and 0 deletions
from flask import Flask, render_template
import feedparser
app = Flask(__name__)
BBC_FEED = "http://feeds.bbci.co.uk/news/rss.xml"
@app.route("/")
def headlines():
feed = feedparser.parse(BBC_FEED)
articles = feed['entries']
return render_template('headlines.html',articles=articles)
@app.route("/<word>")
def headlines_word(word):
word = word.lower()
feed = feedparser.parse(BBC_FEED)
articles = feed['entries']
return render_template('headlines_if.html',articles=articles,word=word)
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return '<center><h1> Hello, World! </h1></center>'
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def hello_world():
greeting = "hello, world!"
return render_template('hello_world.html',greeting=greeting)
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
{% extends "base.html" %}
{% block body %}
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6"><h1>{{ greeting }}</h1></div>
<div class="col-md-3"></div>
</div>
{% endblock %}
import whats_my_name
print 'if_name = {}'.format( __name__)
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
@app.route('/home')
def home():
greeting = "home!"
return render_template('macros_page.html',greeting=greeting)
@app.route('/about')
def about():
greeting = "about!"
return render_template('macros_page.html',greeting=greeting)
@app.route('/contact')
def contact():
greeting = "contact!"
return render_template('macros_page.html',greeting=greeting)
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
from flask import Flask
import feedparser
from random import randint
app = Flask(__name__)
BBC_FEED = "http://feeds.bbci.co.uk/news/rss.xml"
@app.route("/")
def headline():
i = randint(0,9)
feed = feedparser.parse(BBC_FEED)
article = feed['entries'][i]
return """<html>
<body>
<h1> BBC headline </h1>
<b>{0}</b> <br/>
<i>{1}</i> <br/>
<p>{2}</p> <br/>
</body>
</html>""".format(article.get("title"),
article.get("published"), article.get("summary"))
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
from flask import Flask
import random
import string
app = Flask(__name__)
words = ['a','random', 'word','list']
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/word')
def word():
word = ''.join([random.choice(string.ascii_letters) for n in range(32)])
print(word)
return word
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
from flask import Flask
import datetime
app = Flask(__name__)
@app.route('/')
def hello_world():
return '<center><h1> Hello, World! </h1></center>'
@app.route('/time')
def show_time():
t = str(datetime.datetime.now())
return '<center><h1> %s </h1></center>' % t
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
from flask import Flask, render_template
import datetime
app = Flask(__name__)
@app.route('/')
def hello_world():
return '<center><h1> Hello, World! </h1></center>'
@app.template_filter()
def datetimefilter(value, format='%Y/%m/%d %H:%M:%S'):
return value.strftime(format)
app.jinja_env.filters['datetimefilter'] = datetimefilter
@app.route('/time')
def show_time():
return render_template('show_time_with_filter.html', time=datetime.datetime.now())
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
base (how low can you go...)
<div class="jumbotron">
{% block body %}{% endblock %}
</div>
</div>
</body>
</html>
<html>
<head>
<title>Headlines</title>
</head>
<body>
<h1>Headlines</h1>
{% for article in articles %}
<b>{{article.title}}</b><br />
<i>{{article.published}}</i><br />
<p>{{article.summary}}</p>
<hr />
{% endfor %}
</body>
</html>
<html>
<head>
<title>Headlines</title>
</head>
<body>
<h1>Headlines</h1>
{% for article in articles %}
{% if word in article.summary.lower() %}
<b>{{article.title}}</b><br />
<i>{{article.published}}</i><br />
<p>{{article.summary}}</p>
<hr />
{% endif %}
{% endfor %}
</body>
</html>
{% extends "base.html" %}
{% block body %}
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6"><h1>{{ greeting }}</h1></div>
<div class="col-md-3"></div>
</div>
{% endblock %}
{# templates/macros.html #}
{% macro nav_link(endpoint, text) %}
{% if request.endpoint.endswith(endpoint) %}
<li class="active"><a href="{{ url_for(endpoint) }}">{{text}}</a></li>
{% else %}
<li><a href="{{ url_for(endpoint) }}">{{text}}</a></li>
{% endif %}
{% endmacro %}
{% from "macros.html" import nav_link with context %}
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script
src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style type="text/css">
.active {color: red;}
</style>
</head>
<body>
<div class="container">
<ul class="nav-list">
{{ nav_link('home', 'Home') }}
{{ nav_link('about', 'About') }}
{{ nav_link('contact', 'Get in touch') }}
</ul>
<div class="">
{% block body %}{% endblock %}
</div>
</div>
</body>
</html>
{% extends "base.html" %}
{% block body %}
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6"><h1>{{ greeting }}</h1></div>
<div class="col-md-3"></div>
</div>
{% endblock %}
{% extends "base.html" %}
{% block body %}
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6"><h1>{{ time | datetimefilter }}</h1></div>
<div class="col-md-3"></div>
</div>
{% endblock %}
from flask import Flask, render_template
from flask import redirect, url_for
app = Flask(__name__)
@app.route('/')
def home():
greeting = "this route is root"
return render_template('macros_page.html',greeting=greeting)
@app.route('/login')
def login():
# doing some login stuff
return redirect(url_for('home'))
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
from flask import Flask, render_template
from flask import redirect, url_for
from vs_url_for import vs_url_for
app = Flask(__name__)
@app.route('/')
def home():
greeting = "this route is root"
return render_template('macros_page.html',greeting=greeting)
@app.route('/login')
def login():
# doing some login stuff
return redirect(vs_url_for('home'))
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)