Commit 5c3f1c2e authored by CedricPump's avatar CedricPump
Browse files

- added plot

parent 2cc8e7b5
Pipeline #38140 passed with stages
in 1 minute and 22 seconds
.idea/ .idea/
token.txt token.txt
api/plot.svg
import flask import flask
from flask import request, jsonify from flask import request, jsonify
import sqlite3 import sqlite3
import matplotlib.pyplot as plt
import numpy as np
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.config["DEBUG"] = True app.config["DEBUG"] = True
...@@ -67,4 +69,34 @@ def api_filter(): ...@@ -67,4 +69,34 @@ def api_filter():
return jsonify(results) return jsonify(results)
@app.route('/api/v1/resources/books/plot', methods=['GET'])
def book_plot():
db = sqlite3.connect('books.db')
db.row_factory = dict_factory
cur = db.cursor()
books = cur.execute('SELECT * FROM books;').fetchall()
seq = np.array([len(book['first_sentence']) for book in books])
pub = np.array([book['published'] for book in books])
plt.figure()
# add pionts
plt.scatter(pub, seq)
# add linear kurve
fac, off = np.polyfit(pub, seq, 1)
plt.plot(pub, fac * pub + off)
# add labels
plt.xlabel("date of publication")
plt.ylabel("length of first sentence in cahrs")
# create and open svg
plt.savefig("plot.svg")
svg = open("plot.svg").read()
return flask.Response(svg, mimetype='image/svg+xml')
app.run() app.run()
\ 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