Skip to content

Commit

Permalink
ch09
Browse files Browse the repository at this point in the history
  • Loading branch information
rasbt committed Jun 15, 2019
1 parent c9edc8b commit db3a51a
Show file tree
Hide file tree
Showing 43 changed files with 1,958 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ch09/1st_flask_app_1/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
return render_template('first_app.html')

if __name__ == '__main__':
app.run(debug=True)
13 changes: 13 additions & 0 deletions ch09/1st_flask_app_1/templates/first_app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html>
<head>
<title>First app</title>
</head>
<body>

<div>
Hi, this is my first Flask web app!
</div>

</body>
</html>
23 changes: 23 additions & 0 deletions ch09/1st_flask_app_2/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from flask import Flask, render_template, request
from wtforms import Form, TextAreaField, validators

app = Flask(__name__)

class HelloForm(Form):
sayhello = TextAreaField('',[validators.DataRequired()])

@app.route('/')
def index():
form = HelloForm(request.form)
return render_template('first_app.html', form=form)

@app.route('/hello', methods=['POST'])
def hello():
form = HelloForm(request.form)
if request.method == 'POST' and form.validate():
name = request.form['sayhello']
return render_template('hello.html', name=name)
return render_template('first_app.html', form=form)

if __name__ == '__main__':
app.run(debug=True)
3 changes: 3 additions & 0 deletions ch09/1st_flask_app_2/static/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
font-size: 2em;
}
13 changes: 13 additions & 0 deletions ch09/1st_flask_app_2/templates/_formhelpers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% macro render_field(field) %}
<dt>{{ field.label }}
<dd>{{ field(**kwargs)|safe }}
{% if field.errors %}
<ul class=errors>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</dd>
</dt>
{% endmacro %}
23 changes: 23 additions & 0 deletions ch09/1st_flask_app_2/templates/first_app.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!doctype html>
<html>
<head>
<title>First app</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>

{% from "_formhelpers.html" import render_field %}

<div>What's your name?</div>
<form method=post action="/hello">

<dl>
{{ render_field(form.sayhello) }}
</dl>

<input type=submit value='Say Hello' name='submit_btn'>

</form>

</body>
</html>
13 changes: 13 additions & 0 deletions ch09/1st_flask_app_2/templates/hello.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html>
<head>
<title>First app</title>
<link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
</head>
<body>

<div>Hello {{ name }}</div>


</body>
</html>
41 changes: 41 additions & 0 deletions ch09/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Sebastian Raschka, 2017

Python Machine Learning - Code Examples

## Chapter 9 - Embedding a Machine Learning Model into a Web Application

- Serializing fitted scikit-learn estimators
- Setting up a SQLite database for data storage
- Developing a web application with Flask
- Our first Flask web application
- Form validation and rendering
- Turning the movie classifier into a web application
- Deploying the web application to a public server
- Updating the movie review classifier
- Summary

---

The code for the Flask web applications can be found in the following directories:

- `1st_flask_app_1/`: A simple Flask web app
- `1st_flask_app_2/`: `1st_flask_app_1` extended with flexible form validation and rendering
- `movieclassifier/`: The movie classifier embedded in a web application
- `movieclassifier_with_update/`: same as `movieclassifier` but with update from sqlite database upon start


To run the web applications locally, `cd` into the respective directory (as listed above) and execute the main-application script, for example,

cd ./1st_flask_app_1
python3 app.py

Now, you should see something like

* Running on http://127.0.0.1:5000/
* Restarting with reloader

in your terminal.
Next, open a web browser and enter the address displayed in your terminal (typically http://127.0.0.1:5000/) to view the web application.


**Link to a live example application built with this tutorial: http://raschkas.pythonanywhere.com/**.
Loading

0 comments on commit db3a51a

Please sign in to comment.