09. Jupyter Notebook¶
ehemalige Veranstaltungen ia3.netz + ia3.data sowie ia4.Netz im Studiengang Interaktive Medien
(c) 2020/2021 Hochschule Augsburg - Fakultät für Informatik - Prof.Dr.Nik Klever
In [4]:
%%writefile "Aufgabe 8/flask_aufgabe_8.py"
from flask import Flask, render_template, send_file
from os import listdir, path
app = Flask(__name__)
app.debug = True
app.env = "development"
app.path = "./"
@app.route("/",defaults={"path_dir":""})
@app.route("/<path:path_dir>")
def show_dir(path_dir):
app.path = path.join(app.path,path_dir)
if path.isfile(app.path):
return send_file(app.path)
else:
files = listdir(app.path)
return render_template("files.html",files=files)
if __name__ == "__main__":
app.run()
Overwriting Aufgabe 8/flask_aufgabe_8.py
In [3]:
%%writefile "Aufgabe 8/templates/files.html"
<html>
<head>
</head>
<body>
<ul>
{% for file in files %}
<li><a href="{{ file }}">{{ file }}</a></li>
{% endfor %}
</ul>
</body>
</html>
Writing Aufgabe 8/templates/files.html
In [6]:
%%writefile "Aufgabe 8/flask_aufgabe_8a.py"
from flask import Flask, render_template, send_file, redirect, url_for
from os import listdir, path
app = Flask(__name__)
app.debug = True
app.env = "development"
app.path = "./"
@app.route("/",defaults={"path_dir":""})
@app.route("/<path:path_dir>")
def show_dir(path_dir):
app.path = path.join(app.path,path_dir)
if path.isfile(app.path):
return send_file(app.path)
else:
files = listdir(app.path)
for file in files:
if file == "index.html":
return send_file(path.join(app.path,"index.html"))
return render_template("files.html",files=files)
@app.route("/user/<user>")
def userpage(user):
app.path = path.join("./",user,"WWW")
return redirect(url_for("show_dir",path_dir=""))
if __name__ == "__main__":
app.run()
Overwriting Aufgabe 8/flask_aufgabe_8a.py
In [21]:
%%writefile "Aufgabe 8/flask_aufgabe_8b.py"
from flask import Flask, render_template, send_file, redirect, url_for, abort
from os import listdir, path
app = Flask(__name__)
app.debug = True
app.env = "development"
@app.route("/",defaults={"path_dir":"","user":""})
@app.route("/homes/<user>/",defaults={"path_dir":""})
@app.route("/homes/<user>/<path:path_dir>")
def show_dir(user,path_dir=""):
print("show_dir:",user,path_dir)
if user:
app.path = path.join(".",user,"WWW")
print(app.path)
if path.exists(app.path):
app.path = path.join(app.path,path_dir)
print(app.path)
if path.isfile(app.path):
return send_file(app.path)
elif path.isdir(app.path):
files = listdir(app.path)
if files:
if "index.html" in files:
return send_file(path.join(app.path,"index.html"))
else:
return render_template("filesCss.html",files=files,dirName=path_dir)
else:
return abort(403)
else:
return abort(404)
else:
return abort(403)
else:
return render_template("hsa_index.html")
if __name__ == "__main__":
app.run()
Overwriting Aufgabe 8/flask_aufgabe_8b.py
In [20]:
%%writefile "Aufgabe 8/templates/filesCss.html"
<html>
<head>
<title>CSV Datei</title>
<style type="text/css">
.even { background-color:#ffffff; }
.odd { background-color:#e0e0e0; }
</style>
</head>
<body>
<h1>Directory "{{ dirName }}"</h1>
<ul>
{% for file in files %}
{% if not file.startswith(".") %}
<li class = "{{loop.cycle('odd','even')}}"> <a href="{{ dirName }}/{{ file }}">{{ file }}</a> </li>
{% endif %}
{% endfor %}
</ul>
</body>
</html>
Overwriting Aufgabe 8/templates/filesCss.html
In [28]:
%%writefile "Aufgabe 8/flask_aufgabe_8c.py"
from flask import Flask, render_template, send_file, redirect, \
url_for, abort, make_response
from os import listdir, path
app = Flask(__name__)
app.debug = True
app.env = "development"
def leseDatei(dateiname):
try:
datei = open(dateiname,"rb")
gesamterText = datei.read()
datei.close()
return gesamterText
except IOError:
return "Die Datei {} wurde nicht gefunden".format(dateiname)
@app.route("/",defaults={"path_dir":"","user":""})
@app.route("/homes/<user>/",defaults={"path_dir":""})
@app.route("/homes/<user>/<path:path_dir>")
def show_dir(user,path_dir=""):
print("show_dir:",user,path_dir)
if user:
app.path = path.join(".",user,"WWW")
print(app.path)
if path.exists(app.path):
app.path = path.join(app.path,path_dir)
print(app.path)
if path.isfile(app.path):
if app.path.endswith(".png"):
resp = make_response(leseDatei(app.path), 200)
resp.headers["Content-type"] = "image/png"
return resp
else:
return leseDatei(app.path)
elif path.isdir(app.path):
files = listdir(app.path)
if files:
if "index.html" in files:
return leseDatei(path.join(app.path,"index.html"))
else:
return render_template("filesCss.html",files=files,dirName=path_dir)
else:
return abort(403)
else:
return abort(404)
else:
return abort(403)
else:
return render_template("hsa_index.html")
if __name__ == "__main__":
app.run()
Overwriting Aufgabe 8/flask_aufgabe_8c.py
In [24]:
with open("Aufgabe 8/fritz/WWW/logo.png","rb") as f:
content = f.read()
In [25]:
content
Out[25]:
b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\xd0\x00\x00\x008\x08\x06\x00\x00\x00\x8c\xb3v\x9b\x00\x00\x00\x01sRGB\x00\xae\xce\x1c\xe9\x00\x00\x16\xdcIDATx\x01\xed\x9d\x0bxTE\x96\x80\xef\xedN Iw\x14\x94\xa7\xba\x02\xa2\xbcA\x85\xf1\x81\xe2(\xa3\xa3\x8c\xc3\x10EaDaa\xbf\xf8\xfe\x06W]\xd7$<$#0\t#8\x08\xee\x80\xc2HD\x01\x17Q\xd4qp\x9c\x9d]a@Qf\x00Q\x11"\xa0\xbc\xc5!\x82\x907I\xfa\xee\x7f\xda\xbe7uo\xba;\x1d\xd2!\xc1\xdc\xfa\xbe\x9b\xaa:u\xea\xd4\xa9s\xebT\x9d:U}\xa3kM(dgg\'\x95\x96\x96\xa6k\x9a\xde_\xd3\x8c]\xba\xae\xcf\xcb\xcd\xcd=\xda\x84XtYq%`\x93\x80n\xcb5bf\xce\x9c9-\x0f\x1c8\xb0\xda0\xb4+\x156\x0e\xa6\xa4$\x0f@\xb1\x0e)07\xe9J\xa0\xc9H\xc0\xd3T89x\xf0\xe0X\x87\xf2\x08k\xe7\xb0"e7\x15\x1e]>\\\t8%\xd0d\x14(\x10\xd0.u2\xf7}^\xffQx\xb8\x0bu%\xd0\xf8\x12h\x10\x052\x9e\xea\xe73\x9e\x1e\x98\\\x97\xeey<\xda\xee\xf0\xf8\xc6\x9e\xf0p\x17\xeaJ\xa0\xf1%\x90 ,\xb0\xc7\xf0\x1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x92\x86\xd6G\xd0\xfcT\x853\xc8j5\xe3\xd0S\x9b\xf9\x06\x8d*\x06\xb3(y\x83\x05\x94\xe7q\xfaa\x99\x8c\xb49\x9fI\x81\x8b\xc76\xe5\xb1\xda\xa7\xec\xbb\x94\x94\x94\xdb\x00\xa8\xb2\x18}\xeb\xad\xb7v\xb3\x90j&l+\x1f\xedM\x16\x14\xdax\x8fU\xb17\xe2\x9d\x8f\xec?A\xe9KP\xdc\x7fJY\xd0\x84\x13&\xf4\x91\xaf\xda*KaS\t\x8d\xb1\x17k\xcc\xbe\xf3>\x9c\n$/qC\x8b\x16-\xee\xa9\x8d/^\xb0\x98M\xc1\x17o\xe22\x10:\x1e:t\xc8fr\x99e\xe0.0\xd3\x12\x83;r\xfc\xf8\xf1-UX\x98\xb4m\xf5\xa1\xfc]V\xad\xaf\xc3\xe0\xc5\x05\xc4>\xae\x05|\x8d7\x89\xc1s!\xb2x\xc2\xccG\x8a\x97-[V\x00\xae\xa5\xd8\xd0\xf0\xe0$\x10\x87@\xa4\xe0\xd4\x01\x91\xc3a\x14q\x14\n\xf3m\xb8JA\x05\nW\xe0\xc2\x1aO\x02\xbch\xe7\x8b\x14f\x9e\xc2Cv"\x16\xae\x18\\/\x80\'&\x87\x15\x18Ha\x15\x88\xc1\xf12H\xe2\x015C\xeb}\xfb\xf6\r53\x11b\x9b\x02\xc1o^\x04\xbc\xb8\x80\xd9\xc7\xc9\xb1J{\x93\x18}y\x06Y\x88\x97\xac\xd6\x80\xb9\xfb\x07\x07\x92\x98\xb9a\x03tkl_\x80MF\x11m\xb2T+\xbb\n\xa4J\xa3\x89\xa4\x19\x90\xaaM/\\\x15\xb0_x3V\xf6\x18\\\xa5\xbc\xf89\x0e|~\xf4\x88\x9f\xd5\x11\x96.]*&\xce\n\x15\xcc,=F\xcd\xab\xe9\x90\t\xd4O\x81\x1d\xed\xd4\xa9\xd3[J>\xeeI\xfar\xa3J\x94~\xbc\xad\xe6\xa3\xa5\xfb\xf6\xed\xfb\x19\xf5\xad\t\x89\xba}"\xad\xb0\xf4\xdb&w\xa9\xc7\x04\xf3Z4\xfa\x96\x13!\x1a\x92[\xd6\xb8\x12\xe0En\x8au\xb3mr\xca@Yg\xa6%&\x7f\xd6\xed\xb7\xdf\xde\x85\xe4\x97*<\x94\x163\xce\xf2\xc0\xd1\xde\xcd(\xca\xd9\xe1\xcc\x16\x06\x99m\xf5\x01wY\x94\xb3\xa60M\xd5\x1d\x04\xef\x83\xccZ\xb4W\xca\xf9\xd6\xc7\xdc8\xa8\xcb\xe4/+\x88\x1c\x84JH\xd8\xbf\x7f\xff\x05\xc4\xdb\x82\xb9\xe8\x7f6\x88\x19\x18\r\xc5U\xa0h\xd2i"e\x0c\xa0]ue\x05\xd3\xe5K\xce?l\xd5\xc8\xb7\x05PC\x81\xd8\x1c\xff\r\x17\xf1\x17\xb4\x13\xdc`\x13\'\xa2(r\xce\xf2{\x1b\x012\x94\xd9\x14\x88\xfc"\'N<\xf3\xe2\xfc`\xff\x16t\xa2\x08]\xdaK\xde\xb3gO\x19\xcfI7C\xdfL\xcfZT\x1a\xb4\xb5;*\x02\x85u\xd1\xe2\xdah\xb9\xe5\r\'\x01\xcb\x04\x89\xb5\t\x9c\t\x07\xc0\xb5\xdc\xb2\xa1z\xea\xf9\x89\x93\x94\xd3\x99P\xc3\x8c\x0b\x99o\x17+\x15\xb7\xa2|\xffP\xf2qO\x16\x16\x16\x9e\x15w\xa21\x12d\xb5\xabu\x9f\xe5*P\x8c\xc2<\xdd\xd0x\xf9b\xcf\x1fW\xf9\x06f_\x92\x94B\x1c\x0f/R^\xa1\x80\xae\xe4\n\x8c5\xf3\x0b<\x8c\xf9\x96\xa7\xe07H\x92C\xe4\x98V\x8b\xba4\xce\xea|$F\xfcZ\'.W\x81b\x94\xa4\x13\x8ds\x8f&/;\x14\xe2L\x95oX\x8eh\xcf\x8bW\x0b\x93\xe5\r\x15\x1f\x93\xcf\xda\x17\t\x9cr\xcb|\x83v\x15J\'\x1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cc\xb8\x87y\xe7I\x9c\xc0\xbf\xc4\x8a}Jn\x9c\x9c\x0er\x89\x95GK\x81\xe6\xcf\x9f\xbf\x94\xd5dT\xd8\x8a\xba\xb6\xfa\xfe\xfb\xef\x1f\x1c\xb6\xcc\x05\xba\x12h\xc6\x12\xa8\xde\x03\x19\xda\x90\x88r0\xb4A\x8b\x17/\x96\r\xb9\x1b\\\t\xb8\x12P$P\xad@\x8a\xff_)7\x93\xf2/\x03\xad\xf3\x01\x13\xe8\xc6\xae\x04\x9a\xbb\x04,\x05b\xaf\xf3aDa\xe8\xda\x8e\xb1c\xc7\xca\x0f\x95\xdc\xe0J\xc0\x95\x80"\x01K\x81\xf0\x9f=\x01<\xacg\x0c\xe5\x9a\xa8\xd4q\x93\xae\x04\\\t\x84$`)\x10\xae\xeau\x1e\xafg\x18g<{L\xe9\x90>\x8cb\x8d\xa1\xecU\x13\xe6\xc6\xae\x04\\\tTK\xc0\xf2\xc2\x99 \xf6::\xe7A\xf2C-/\x1f\xca\xd86r\xe4\xc8\x06\xbf\x07g\xb6\xed\xc6\xae\x04N7\t\xfc?\xf6\xc0\'o\xbb<\xe2r\x00\x00\x00\x00IEND\xaeB`\x82'
In [35]:
%%writefile "Aufgabe 8/flask_aufgabe_8d.py"
from flask import Flask, render_template, send_file, redirect, \
url_for, abort, make_response
from os import listdir, path
app = Flask(__name__)
app.debug = True
app.env = "development"
@app.route("/",defaults={"path_dir":"","user":""})
@app.route("/homes/<user>/",defaults={"path_dir":""})
@app.route("/homes/<user>/<path:path_dir>")
def show_dir(user,path_dir=""):
app.logger.info("show_dir({},{})".format(user,path_dir))
if user:
app.path = path.join(".",user,"WWW")
if path.exists(app.path):
app.path = path.join(app.path,path_dir)
if path.isfile(app.path):
return send_file(app.path)
elif path.isdir(app.path):
files = listdir(app.path)
if files:
if "index.html" in files:
return send_file(path.join(app.path,"index.html"))
else:
return render_template("filesCss.html",files=files,dirName=path_dir)
else:
app.logger.info("abort(403) no files in directory")
return abort(403)
else:
app.logger.info("abort(404) not found")
return abort(404)
else:
app.logger.info("abort(403) no www dir")
return abort(403)
else:
app.logger.info("hsa index - no user")
return render_template("hsa_index.html")
if __name__ == "__main__":
app.run()
Overwriting Aufgabe 8/flask_aufgabe_8d.py
In [34]:
%%writefile "Aufgabe 8/templates/filesCss.html"
<html>
<head>
<title>CSV Datei</title>
<style type="text/css">
.even { background-color:#ffffff; }
.odd { background-color:#e0e0e0; }
</style>
</head>
<body>
<h1>Directory "{{ dirName }}"</h1>
<ul>
{% for file in files %}
{% if not file.startswith(".") %}
{% if dirName.endswith("/") %}
<li class = "{{loop.cycle('odd','even')}}"> <a href="{{ file }}">{{ file }}</a> </li>
{% else %}
<li class = "{{loop.cycle('odd','even')}}"> <a href="{{ dirName }}/{{ file }}">{{ file }}</a> </li>
{% endif %}
{% endif %}
{% endfor %}
</ul>
</body>
</html>
Overwriting Aufgabe 8/templates/filesCss.html
In [50]:
%%writefile "Aufgabe 8/flask_aufgabe_8e.py"
from flask import Flask, render_template, send_file, redirect, flash, \
url_for, abort, make_response, request, session
from os import listdir, path
app = Flask(__name__)
app.debug = True
app.env = "development"
app.secret_key = "agkldjhgvc xkfvo iososigshois218408275"
users = dict()
with open("password.csv") as f:
content = f.read()
for line in content.split("\n"):
user,pw = line.split(";")
users.update({user:pw})
app.logger.info("Users: {}".format(users))
@app.route("/")
def index():
return redirect(url_for("login"))
@app.route("/homes/<user>/",defaults={"path_dir":""})
@app.route("/homes/<user>/<path:path_dir>")
def show_dir(user,path_dir=""):
app.logger.info("show_dir({},{})".format(user,path_dir))
if user:
app.path = path.join(".",user,"WWW")
if path.exists(app.path):
app.path = path.join(app.path,path_dir)
if path.isfile(app.path):
return send_file(app.path)
elif path.isdir(app.path):
files = listdir(app.path)
if files:
if "index.html" in files:
return send_file(path.join(app.path,"index.html"))
else:
return render_template("filesCss.html",files=files,dirName=path_dir)
else:
app.logger.info("abort(403) no files in directory")
return abort(403)
else:
app.logger.info("abort(404) not found")
return abort(404)
else:
app.logger.info("abort(403) no www dir")
return abort(403)
else:
app.logger.info("hsa index - no user")
return render_template("hsa_index.html")
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] not in users.keys():
error = 'Invalid username'
else:
if request.form['password'] != users[request.form['username']]:
error = 'Invalid password'
else:
session['logged_in'] = request.form['username']
flash('You were logged in')
return redirect(url_for('show_dir',user=request.form['username']))
return render_template('login.html', error=error)
@app.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You were logged out')
return redirect(url_for('index'))
if __name__ == "__main__":
app.run()
Overwriting Aufgabe 8/flask_aufgabe_8e.py
In [40]:
%%writefile "Aufgabe 8/templates/login.html"
{% extends "layout.html" %}
{% block body %}
<h2>Login</h2>
{% if error %}<p class=error><strong>Error:</strong> {{ error }}{% endif %}
<form action="{{ url_for('login') }}" method=post>
<dl>
<dt>Username:
<dd><input type=text name=username>
<dt>Password:
<dd><input type=password name=password>
<dd><input type=submit value=Login>
</dl>
</form>
{% endblock %}
Writing Aufgabe 8/templates/login.html
In [42]:
%%writefile "Aufgabe 8/templates/layout.html"
<!doctype html>
<title>Apache Simulation</title>
<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">
<div class=page>
<h1>Apache Simulation</h1>
<div class=metanav>
{% if not session.logged_in %}
<a href="{{ url_for('login') }}">log in</a>
{% else %}
<a href="{{ url_for('logout') }}">log out</a>
{% endif %}
</div>
{% for message in get_flashed_messages() %}
<div class=flash>{{ message }}</div>
{% endfor %}
{% block body %}{% endblock %}
</div>
Overwriting Aufgabe 8/templates/layout.html
In [43]:
%%writefile "Aufgabe 8/static/style.css"
body { font-family: sans-serif; background: #eee; }
a, h1, h2 { color: #377ba8; }
h1, h2 { font-family: 'Georgia', serif; margin: 0; }
h1 { border-bottom: 2px solid #eee; }
h2 { font-size: 1.2em; }
.page { margin: 2em auto; width: 35em; border: 5px solid #ccc;
padding: 0.8em; background: white; }
.entries { list-style: none; margin: 0; padding: 0; }
.entries li { margin: 0.8em 1.2em; }
.entries li h2 { margin-left: -1em; }
.add-entry { font-size: 0.9em; border-bottom: 1px solid #ccc; }
.add-entry dl { font-weight: bold; }
.metanav { text-align: right; font-size: 0.8em; padding: 0.3em;
margin-bottom: 1em; background: #fafafa; }
.flash { background: #cee5F5; padding: 0.5em;
border: 1px solid #aacbe2; }
.error { background: #f0d6d6; padding: 0.5em; }
Writing Aufgabe 8/static/style.css
In [54]:
%%writefile "Aufgabe 8/flask_aufgabe_8f.py"
from flask import Flask, render_template, send_file, redirect, flash, \
url_for, abort, make_response, request, session, Markup
from os import listdir, path
app = Flask(__name__)
app.debug = True
app.env = "development"
app.secret_key = "agkldjhgvc xkfvo iososigshois218408275"
users = dict()
with open("password.csv") as f:
content = f.read()
for line in content.split("\n"):
user,pw = line.split(";")
users.update({user:pw})
app.logger.info("Users: {}".format(users))
@app.route("/")
def index():
return redirect(url_for("login"))
@app.route("/homes/<user>/",defaults={"path_dir":""})
@app.route("/homes/<user>/<path:path_dir>")
def show_dir(user,path_dir=""):
app.logger.info("show_dir({},{})".format(user,path_dir))
if user:
app.path = path.join(".",user,"WWW")
if path.exists(app.path):
app.path = path.join(app.path,path_dir)
if path.isfile(app.path):
return send_file(app.path)
elif path.isdir(app.path):
files = listdir(app.path)
if files:
if "index.html" in files:
return send_file(path.join(app.path,"index.html"))
else:
return render_template("filesCss.html",files=files,dirName=path_dir)
else:
app.logger.info("abort(403) no files in directory")
return abort(403)
else:
app.logger.info("abort(404) not found")
return abort(404)
else:
app.logger.info("abort(403) no www dir")
return abort(403)
else:
app.logger.info("hsa index - no user")
return render_template("hsa_index.html")
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
if request.method == 'GET':
return render_template('upload.html')
elif request.method == 'POST':
f = request.files['upload']
dateiSpeicher = f.stream.read().decode('utf-8')
if f.filename.endswith(".csv"):
dateiZeilen = dateiSpeicher.split('\n')[:-1]
return render_template('uploadfile.html', dateiZeilen = dateiZeilen, dateiName = f.filename)
elif f.filename.endswith(".xml"):
return Markup.escape(dateiSpeicher)
@app.route('/login', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] not in users.keys():
error = 'Invalid username'
else:
if request.form['password'] != users[request.form['username']]:
error = 'Invalid password'
else:
session['logged_in'] = request.form['username']
flash('You were logged in')
return redirect(url_for('show_dir',user=request.form['username']))
return render_template('login.html', error=error)
@app.route('/logout')
def logout():
session.pop('logged_in', None)
flash('You were logged out')
return redirect(url_for('index'))
if __name__ == "__main__":
app.run()
Overwriting Aufgabe 8/flask_aufgabe_8f.py
In [51]:
%%writefile "Aufgabe 8/templates/upload.html
<html>
<head>
<title>Upload</title>
</head>
<body>
<h1>Upload</h1>
<form ACTION="/upload" method="POST" enctype ="multipart/form-data">
<p><input type="file" name="upload"/></p>
<p><input type="submit" value="Datei hochladen"/></p>
</form>
<p>Vielen Dank!</p>
</body>
</html>
Writing Aufgabe 8/templates/upload.html
In [53]:
%%writefile "Aufgabe 8/templates/uploadfile.html"
<html>
<head>
<title>CSV Datei</title>
<style type="text/css">
.even { background-color:#ffffff; }
.odd { background-color:#e0e0e0; }
</style>
</head>
<body>
<h1>CSV Datei "{{ dateiName }}"</h1>
<ul>
{% for dateiZeile in dateiZeilen %}
<li class = "{{loop.cycle('odd','even')}}"> {{ loop.index}}. Zeile = "{{ dateiZeile }}" </li>
{% endfor %}
</ul>
</body>
</html>
Writing Aufgabe 8/templates/uploadfile.html