Commit 10a382be authored by danmcquillan's avatar danmcquillan

added lab-12

parent acf8c244
def a_decorator(a_function):
def wrapper():
print("this gets done before a_function() is called.")
a_function()
print("this gets done after a_function() is called.")
return wrapper
def an_actual_function():
print("i am an_actual_function!")
an_actual_function = a_decorator(an_actual_function)
an_actual_function()
import time
def timing_function(some_function):
"""
Outputs the time a function takes
to execute.
"""
def wrapper():
t1 = time.time()
some_function()
t2 = time.time()
return "Time it took to run the function: " + str((t2 - t1)) + "\n"
return wrapper
@timing_function
def my_function():
num_list = []
for num in (range(0, 10000)):
num_list.append(num)
print("\nSum of all the numbers: " + str((sum(num_list))))
print(my_function())
from flask import Flask
import feedparser # this module reads the rss feeds
from random import randint
app = Flask(__name__)
BBC_FEED = "http://feeds.bbci.co.uk/news/rss.xml"
@app.route("/")
def headline():
feed = feedparser.parse(BBC_FEED)
article = feed['entries'][0]
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, 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)
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
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)
from flask import Flask
import datetime
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
@app.route('/<username>')
def show_user_profile(username):
# show the user profile for that user
return '<center><h1> hello, %s </h1></center>' % username
if __name__ == '__main__':
app.run(debug=True,host='0.0.0.0',port=8000)
from flask import url_for
URL_PREFIX = '/usr/253'
def vs_url_for(view):
url = url_for(view)
url = URL_PREFIX + url
return url
print 'whats_my_name = {}'.format(__name__)
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