forked from rasbt/python-machine-learning-book-3rd-edition
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
1,958 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
body { | ||
font-size: 2em; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/**. |
Oops, something went wrong.