Skip to content

Commit

Permalink
#3 Make call to backend to get array of random integers and graph thi…
Browse files Browse the repository at this point in the history
…s on a bar chart on the frontend
  • Loading branch information
LeNPaul committed Apr 11, 2020
1 parent f1fab6d commit fbe8c90
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 43 deletions.
10 changes: 7 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import Flask
from flask import render_template
from flask import jsonify
app = Flask(__name__)

from random import randint
Expand All @@ -8,6 +9,9 @@
def index():
return render_template('index.html')

@app.route('/random')
def calculate():
return str(randint(0,20))
@app.route('/random/<size>')
def random(size):
randList = []
for x in range(0, int(size)):
randList.append(randint(0,20))
return jsonify(randList)
87 changes: 50 additions & 37 deletions static/public/assets/js/backend.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,57 @@
function getChart(chartData) {
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: chartData,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}

function generateChart() {
jQuery.get('/random/6', function(data, status) {
getChart(data)
});
}

var app = new Vue({
el: '#app',
data: {
message: 'A basic n-body simulation program that aims to simulate many of the processes that occur in the early stages of the universe.'
},
methods: {
graph: generateChart()
}
})

var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
app.graph()
4 changes: 1 addition & 3 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,9 @@ <h1>applepy</h1>

</div>

</div>

<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<!-- Custom javascript -->
Expand Down

0 comments on commit fbe8c90

Please sign in to comment.