Skip to content

Commit 38aa202

Browse files
committed
initial commit
0 parents  commit 38aa202

File tree

10 files changed

+244
-0
lines changed

10 files changed

+244
-0
lines changed

Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM python
2+
3+
WORKDIR /code
4+
5+
COPY bin .
6+
7+
RUN pip install --upgrade pip
8+
9+
RUN pip install -r requirements.txt
10+
11+
# CMD "python app.py"

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# python-web-matplotlib-spectrogram-graph
2+
3+
## Description
4+
A demo of creating a spectrogram graph.
5+
6+
A spectrogram can be defined as the visual representation of frequencies against time which shows the signal strength at a particular time. In simple words, a spectrogram is nothing but a picture of sound. It is also called voiceprint or voice grams. A spectrogram is shown using many colors which indicates the signal strengths. If the color is bright then it means that the energy of the signal is high. In other words, brightness of the color is directly proportional to the strength of the signal in spectrogram.
7+
8+
## Tech stack
9+
- numpy
10+
- matplotlib
11+
- pandas
12+
13+
## Docker stack
14+
- python:latest
15+
16+
## To run
17+
`sudo ./install.sh -u`
18+
[Availble here](http://localhost)
19+
20+
## To stop
21+
`sudo ./install.sh -d`
22+
23+
## To see help
24+
`sudo ./install.sh -h`

bin/app.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from flask import Flask, render_template, request
2+
import matplotlib.pyplot as plt
3+
import numpy as np
4+
import logging
5+
6+
logging.basicConfig(level=logging.INFO)
7+
8+
app = Flask(
9+
__name__,
10+
instance_relative_config=False,
11+
template_folder="templates"
12+
)
13+
14+
def create_graph():
15+
filename = 'demo'
16+
17+
# random but consistant data
18+
lst = [2,9,4,6,4]
19+
20+
# clear buffer
21+
plt.clf()
22+
plt.specgram(lst, Fs=6, cmap="rainbow")
23+
plt.savefig(f'static/img/{filename}.png')
24+
25+
@app.route('/', methods=['GET'])
26+
def getIndex():
27+
create_graph()
28+
return render_template("index.html")
29+
30+
@app.errorhandler(404)
31+
def page_not_found(e):
32+
return "<h1>404</h1><p>The resource could not be found.</p>", 404
33+
34+
if __name__ == "__main__":
35+
app.run(host ='0.0.0.0', port = 5000, debug = True)

bin/requirements.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Lib for web
2+
flask
3+
4+
# Lib for graph
5+
matplotlib
6+
pandas
7+
numpy

bin/static/img/out.png

33.4 KB
Loading

bin/templates/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{% extends 'layout.html' %}
2+
{% block content %}
3+
<img src="{{ url_for('static', filename='img/demo.png') }}" alt="chart-image" width="auto" height="auto">
4+
{% endblock %}

bin/templates/layout.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE HTML>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
6+
7+
<title>Matplot demo</title>
8+
</head>
9+
<body>
10+
{% include 'nav.html' %}
11+
<div class="container">
12+
{% block content %}{% endblock %}
13+
</div>
14+
</body>

bin/templates/nav.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<!-- this is header -->
2+
<nav class="navbar navbar-inverse static-top">
3+
<a class="navbar-text">Matplot graph demo</a>
4+
</nav>

general.log

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[2024-03-16 23:24:40 INFO]: install::setup-logging ended
2+
================
3+
[2024-03-16 23:24:40 INFO]: install::start-up started
4+
[2024-03-16 23:24:40 INFO]: install::start-up starting services
5+
[2024-03-16 23:24:40 INFO]: install::start-up ended
6+
================

install.sh

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#!/usr/bin/env bash
2+
basefile="install"
3+
logfile="general.log"
4+
timestamp=`date '+%Y-%m-%d %H:%M:%S'`
5+
6+
if [ "$#" -ne 1 ]; then
7+
msg="[ERROR]: $basefile failed to receive enough args"
8+
echo "$msg"
9+
echo "$msg" >> $logfile
10+
exit 1
11+
fi
12+
13+
function setup-logging(){
14+
scope="setup-logging"
15+
info_base="[$timestamp INFO]: $basefile::$scope"
16+
17+
echo "$info_base started" >> $logfile
18+
19+
echo "$info_base removing old logs" >> $logfile
20+
21+
rm -f $logfile
22+
23+
echo "$info_base ended" >> $logfile
24+
25+
echo "================" >> $logfile
26+
}
27+
28+
function root-check(){
29+
scope="root-check"
30+
info_base="[$timestamp INFO]: $basefile::$scope"
31+
32+
echo "$info_base started" >> $logfile
33+
34+
#Make sure the script is running as root.
35+
if [ "$UID" -ne "0" ]; then
36+
echo "[$timestamp ERROR]: $basefile::$scope you must be root to run $0" >> $logfile
37+
echo "==================" >> $logfile
38+
echo "You must be root to run $0. Try the following"
39+
echo "sudo $0"
40+
exit 1
41+
fi
42+
43+
echo "$info_base ended" >> $logfile
44+
echo "================" >> $logfile
45+
}
46+
47+
function docker-check() {
48+
scope="docker-check"
49+
info_base="[$timestamp INFO]: $basefile::$scope"
50+
cmd=`docker -v`
51+
52+
echo "$info_base started" >> $logfile
53+
54+
if [ -z "$cmd" ]; then
55+
echo "$info_base docker not installed"
56+
echo "$info_base docker not installed" >> $logfile
57+
fi
58+
59+
echo "$info_base ended" >> $logfile
60+
echo "================" >> $logfile
61+
62+
}
63+
64+
function docker-compose-check() {
65+
scope="docker-compose-check"
66+
info_base="[$timestamp INFO]: $basefile::$scope"
67+
cmd=`docker-compose -v`
68+
69+
echo "$info_base started" >> $logfile
70+
71+
if [ -z "$cmd" ]; then
72+
echo "$info_base docker-compose not installed"
73+
echo "$info_base docker-compose not installed" >> $logfile
74+
fi
75+
76+
echo "$info_base ended" >> $logfile
77+
echo "================" >> $logfile
78+
79+
}
80+
function usage() {
81+
echo ""
82+
echo "Usage: "
83+
echo ""
84+
echo "-u: start."
85+
echo "-d: tear down."
86+
echo "-h: Display this help and exit."
87+
echo ""
88+
}
89+
function start-up(){
90+
91+
local scope="start-up"
92+
local docker_img_name=`head -n 1 README.md | sed 's/# //'`
93+
local info_base="[$timestamp INFO]: $basefile::$scope"
94+
95+
echo "$info_base started" >> $logfile
96+
97+
echo "$info_base starting services" >> $logfile
98+
99+
docker build -t $docker_img_name .
100+
101+
docker run -ti --rm -p 80:5000 -w /code -v $(pwd)/bin:/code $docker_img_name bash -c "python3 app.py"
102+
103+
echo "$info_base ended" >> $logfile
104+
105+
echo "================" >> $logfile
106+
}
107+
function tear-down(){
108+
109+
scope="tear-down"
110+
info_base="[$timestamp INFO]: $basefile::$scope"
111+
112+
echo "$info_base started" >> $logfile
113+
114+
echo "$info_base starting services" >> $logfile
115+
116+
echo "$info_base ended" >> $logfile
117+
118+
echo "================" >> $logfile
119+
}
120+
121+
root-check
122+
docker-check
123+
docker-compose-check
124+
125+
while getopts ":udh" opts; do
126+
case $opts in
127+
u)
128+
setup-logging
129+
start-up ;;
130+
d)
131+
tear-down ;;
132+
h)
133+
usage
134+
exit 0 ;;
135+
/?)
136+
usage
137+
exit 1 ;;
138+
esac
139+
done

0 commit comments

Comments
 (0)