Verified Commit 1deb2bd0 authored by Lohse, Felix's avatar Lohse, Felix
Browse files

exercise 3

parent 2f5a2a7b
Pipeline #37941 passed with stages
in 1 minute and 13 seconds
import flask
from flask import request, jsonify
import sqlite3
import matplotlib.pyplot as plt
import numpy as np
import string, re
app = flask.Flask(__name__)
......@@ -28,6 +31,26 @@ def api_all():
return jsonify(all_books)
@app.route('/api/v1/resources/books/plot', methods=['GET'])
def book_plot():
conn = sqlite3.connect('books.db')
conn.row_factory = dict_factory
cur = conn.cursor()
all_books = cur.execute('SELECT * FROM books;').fetchall()
ys = np.array([len(book['first_sentence']) for book in all_books])
xs = np.array([book['published'] for book in all_books])
m, b = np.polyfit(xs, ys, 1)
fig = plt.figure()
plt.scatter(xs, ys, color="red")
plt.plot(xs, m * xs + b, color="blue")
plt.xlabel("Date of Publication")
plt.ylabel("Length of First Sentence in Characters")
return flask.Response(open("top-10.svg").read(), mimetype='image/svg+xml')
def page_not_found(e):
......@@ -67,4 +90,5 @@ def api_filter():
return jsonify(results)"")
\ No newline at end of file
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