<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=I719_Fundamentals_of_Python%2Flecture4v2</id>
	<title>I719 Fundamentals of Python/lecture4v2 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.itcollege.ee/index.php?action=history&amp;feed=atom&amp;title=I719_Fundamentals_of_Python%2Flecture4v2"/>
	<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture4v2&amp;action=history"/>
	<updated>2026-06-16T03:38:58Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture4v2&amp;diff=120527&amp;oldid=prev</id>
		<title>Eroman: Created page with &quot;= Lecture 4 =  introduction to website development with flask.  NOTE: you do not need to know this material for the test or to know python in general. It is only for showing h...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.itcollege.ee/index.php?title=I719_Fundamentals_of_Python/lecture4v2&amp;diff=120527&amp;oldid=prev"/>
		<updated>2017-04-27T18:26:39Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;= Lecture 4 =  introduction to website development with flask.  NOTE: you do not need to know this material for the test or to know python in general. It is only for showing h...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Lecture 4 =&lt;br /&gt;
&lt;br /&gt;
introduction to website development with flask.&lt;br /&gt;
&lt;br /&gt;
NOTE: you do not need to know this material for the test or to know python in general. It is only for showing how to make a website easily.&lt;br /&gt;
&lt;br /&gt;
== Task 0 ==&lt;br /&gt;
&lt;br /&gt;
copy and paste the example from flask&amp;#039;s homepage http://flask.pocoo.org/&lt;br /&gt;
&lt;br /&gt;
then run it, and go the the url printed in your terminal&lt;br /&gt;
&lt;br /&gt;
== Query strings and form data ==&lt;br /&gt;
&lt;br /&gt;
The form data and query string data are acessible from a special &amp;lt;code&amp;gt;request&amp;lt;/code&amp;gt; object. This &amp;lt;code&amp;gt;request&amp;lt;/code&amp;gt; object is imported, and only has data when it is used inside of a view function.&lt;br /&gt;
&lt;br /&gt;
the data for query strings and forms is in a dictionary like object.&lt;br /&gt;
&lt;br /&gt;
== Task 1 ==&lt;br /&gt;
&lt;br /&gt;
Show the bitcoin price in Euro on a webpage&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;import requests&lt;br /&gt;
from flask import Flask&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def get_btc_price():&lt;br /&gt;
    r = requests.get(&amp;#039;https://api.coindesk.com/v1/bpi/currentprice.json&amp;#039;)&lt;br /&gt;
    data = r.json()&lt;br /&gt;
    price = data[&amp;#039;bpi&amp;#039;][&amp;#039;EUR&amp;#039;][&amp;#039;rate&amp;#039;]&lt;br /&gt;
    return price&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
app = Flask(__name__)&lt;br /&gt;
app.config[&amp;#039;DEBUG&amp;#039;] = True&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@app.route(&amp;quot;/&amp;quot;)&lt;br /&gt;
def hello():&lt;br /&gt;
    return &amp;#039;{} &amp;amp;euro;&amp;#039;.format(get_btc_price())&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    app.run()&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
== HTML Templates ==&lt;br /&gt;
&lt;br /&gt;
To use an HTML template with the default flask configuration, make a directory &amp;#039;templates&amp;#039; in the same directory as your flask script. you can then put html template in here.&lt;br /&gt;
&lt;br /&gt;
for a script &amp;lt;code&amp;gt;./app.py&amp;lt;/code&amp;gt; to use a template &amp;lt;code&amp;gt;./templates/index.html&amp;lt;/code&amp;gt;, import &amp;lt;code&amp;gt;render_template&amp;lt;/code&amp;gt; from flask: &amp;lt;code&amp;gt;from flask import Flask, render_template&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and use the line in your view function &amp;lt;code&amp;gt;return render_template(&amp;#039;index.html&amp;#039;)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
variables in jinja2 templates look like &amp;lt;code&amp;gt;{{ variable_name }}&amp;lt;/code&amp;gt;, and can be substituted in the &amp;lt;code&amp;gt;render_template&amp;lt;/code&amp;gt; function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;return render_template(&amp;#039;index.html&amp;#039;, variable_name=&amp;#039;value&amp;#039;)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Flask uses the Jinja2 templating engine. see their documentation for more information&lt;br /&gt;
&lt;br /&gt;
== Task 2 ==&lt;br /&gt;
&lt;br /&gt;
Make a form that asks for name and age, and displays:&lt;br /&gt;
&lt;br /&gt;
&amp;#039;Hi $NAME, you are $AGE years old&amp;#039;&lt;br /&gt;
&lt;br /&gt;
using a post request&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;app.py&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;from flask import Flask, render_template, request&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
app = Flask(__name__)&lt;br /&gt;
app.config[&amp;#039;DEBUG&amp;#039;] = True&lt;br /&gt;
&lt;br /&gt;
@app.route(&amp;quot;/&amp;quot;, methods=[&amp;#039;POST&amp;#039;, &amp;#039;GET&amp;#039;])&lt;br /&gt;
def hello():&lt;br /&gt;
    name = request.form.get(&amp;#039;name&amp;#039;, &amp;#039;&amp;#039;)&lt;br /&gt;
    age = request.form.get(&amp;#039;age&amp;#039;, &amp;#039;&amp;#039;)&lt;br /&gt;
    if name and age:&lt;br /&gt;
        return render_template(&amp;#039;index.html&amp;#039;, name=name, age=age)&lt;br /&gt;
    else:&lt;br /&gt;
        return render_template(&amp;#039;index.html&amp;#039;, name=name, age=age), 400&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    app.run(port=5000)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;code&amp;gt;templates/index.html&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre class=&amp;quot;html&amp;quot;&amp;gt;&amp;amp;lt;html&amp;amp;gt;&lt;br /&gt;
    &amp;amp;lt;head&amp;amp;gt;&lt;br /&gt;
    &amp;amp;lt;/head&amp;amp;gt;&lt;br /&gt;
    &amp;amp;lt;body&amp;amp;gt;&lt;br /&gt;
        &amp;amp;lt;h1&amp;amp;gt;HTML!&amp;amp;lt;/h1&amp;amp;gt;&lt;br /&gt;
        &amp;amp;lt;p&amp;amp;gt;Hi {{ name }}, you are {{ age }} years old&amp;amp;lt;/p&amp;amp;gt;&lt;br /&gt;
        &amp;amp;lt;form action=&amp;amp;quot;/&amp;amp;quot; method=&amp;amp;quot;POST&amp;amp;quot;&amp;amp;gt;&lt;br /&gt;
            &amp;amp;lt;label&amp;amp;gt;Age&lt;br /&gt;
                &amp;amp;lt;input type=&amp;amp;quot;text&amp;amp;quot; name=&amp;amp;quot;age&amp;amp;quot; /&amp;amp;gt;&lt;br /&gt;
            &amp;amp;lt;/label&amp;amp;gt;&lt;br /&gt;
            &amp;amp;lt;label&amp;amp;gt;Name&amp;amp;lt;input type=&amp;amp;quot;text&amp;amp;quot; name=&amp;amp;quot;name&amp;amp;quot; /&amp;amp;gt;&amp;amp;lt;/label&amp;amp;gt;&lt;br /&gt;
            &amp;amp;lt;button type=&amp;amp;quot;submit&amp;amp;quot;&amp;amp;gt;submit&amp;amp;lt;/button&amp;amp;gt;&lt;br /&gt;
        &amp;amp;lt;/form&amp;amp;gt;&lt;br /&gt;
    &amp;amp;lt;/body&amp;amp;gt;&lt;br /&gt;
&amp;amp;lt;/html&amp;amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Task 3 ==&lt;br /&gt;
&lt;br /&gt;
make a post request to your application with &amp;lt;code&amp;gt;requests&amp;lt;/code&amp;gt; that submits the name and age&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;r = requests.post(&amp;#039;http://localhost:5000/&amp;#039;, data={&amp;#039;name&amp;#039;: &amp;#039;Talvi&amp;#039;, &amp;#039;age&amp;#039;: 50})&lt;br /&gt;
print(r.text)&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Eroman</name></author>
	</entry>
</feed>