Sunday, August 7, 2022

Verification Central limit theorem via flask&&matplotlib

 boris@boris-All-Series:~/FLASKHTML$ ls -CRl

.:

total 20

-rw-rw-r-- 1 boris boris 1559 авг  7 19:15 flaskMatplotlib10.py

-rw-rw-r-- 1 boris boris 1653 авг  7 19:15 flaskMatplotlib12.py

-rw-rw-r-- 1 boris boris 2060 авг  7 19:15 flaskMatplotlib9.py

drwxrwxr-x 2 boris boris 4096 авг  7 19:21 __pycache__

drwxrwxr-x 2 boris boris 4096 авг  7 19:15 templates

./__pycache__:

total 4

-rw-rw-r-- 1 boris boris 1807 авг  7 19:21 flaskMatplotlib12.cpython-38.pyc

./templates:

total 4

-rw-rw-r-- 1 boris boris 162 авг  7 19:15 download.html


boris@boris-All-Series:~/FLASKHTML$ cat flaskMatplotlib12.py

import io

from flask import Response, render_template

from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

from matplotlib.figure import Figure

from flask import Flask

import matplotlib.pyplot as plt

import numpy as np

import scipy.stats as stats

import warnings

warnings.filterwarnings('ignore')


plt.rcParams["figure.figsize"] = [15.50, 11.50]

plt.rcParams["figure.autolayout"] = True

app = Flask(__name__)


@app.route('/')

def upload_form():

return render_template('download.html')


@app.route('/print-plot')

def plot_png():

   fig = Figure()

   axes = fig.add_subplot(1, 1, 1)

   axes.set_title('Verification CLT via flask&&matplotlib, Refresh Screen a couple of times',fontsize = 15, color = 'black')


   n = 10000

   longlst = []

   sigmalst = []

   lst = ["avg","avg1","avg2","avg3"]

   for avs in lst:

       avs = [] 

       for i in range(1,n):

         a = np.random.randint(1,7,10)

         avs.append(np.average(a))

       # Normalise this histogram too

       axes.hist(avs[0:], 20, density=True, stacked=True)

       longlst = longlst + avs[0:]

       sigmalst = sigmalst + avs[:]

   zscore = stats.zscore(longlst)

   mu, sigma = np.mean(sigmalst), np.std(sigmalst)

   s = np.random.normal(mu, sigma, 10000)

   # Create the bins and histogram

   count, bins, ignored = axes.hist(s, 20, density=True, stacked=True) 

   # Use scipy.stats implementation of the normal pdf

   # Plot the distribution curve

   x = np.linspace(1.5, 6.5, num=100)

   axes.plot(x, stats.norm.pdf(x, mu, sigma))

   output = io.BytesIO()

   FigureCanvas(fig).print_png(output)

   return Response(output.getvalue(), mimetype='image/png')


boris@boris-All-Series:~/FLASKHTML$ cat templates/download.html

<!doctype html>

<title>Python Flask File Generate Gauss CLT Report </title>

<p>

<h2><a href="{{ url_for('.plot_png') }}">Display Gaussian plots</a></h2>

</p>

(.env) boris@boris-All-Series:~/FLASKHTML$ env | grep "FLASK_"
FLASK_APP=flaskMatplotlib12.py
FLASK_DEBUG=1
FLASK_ENV=development
(.env) boris@boris-All-Series:~/FLASKHTML$ ll
total 40
drwxrwxr-x   5 boris boris  4096 авг  7 19:21 ./
drwxr-xr-x 148 boris boris 12288 авг  7 19:33 ../
drwxrwxr-x   6 boris boris  4096 авг  7 19:17 .env/
-rw-rw-r--   1 boris boris  1559 авг  7 19:15 flaskMatplotlib10.py
-rw-rw-r--   1 boris boris  1653 авг  7 19:15 flaskMatplotlib12.py
-rw-rw-r--   1 boris boris  2060 авг  7 19:15 flaskMatplotlib9.py
drwxrwxr-x   2 boris boris  4096 авг  7 19:21 __pycache__/
drwxrwxr-x   2 boris boris  4096 авг  7 19:15 templates/

(.env) boris@boris-All-Series:~/FLASKHTML$ flask run
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Serving Flask app 'flaskMatplotlib12.py'
 * Debug mode: on
 * Running on http://127.0.0.1:5000 (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 424-443-302
127.0.0.1 - - [07/Aug/2022 19:36:47] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [07/Aug/2022 19:37:06] "GET /print-plot HTTP/1.1" 200 -
127.0.0.1 - - [07/Aug/2022 19:37:11] "GET /print-plot HTTP/1.1" 200 -
127.0.0.1 - - [07/Aug/2022 19:37:16] "GET /print-plot HTTP/1.1" 200 -
127.0.0.1 - - [07/Aug/2022 19:37:22] "GET /print-plot HTTP/1.1" 200 -












































No comments:

Post a Comment