Hi Everybody,
I am quite new to Django, so I request for your kind assistance and
guidance.
I have a machine learning model which as of now gives the output in csv
format. I want to read that output file which gets generated after every 1
hour or maybe 1 day, and plot a chart using it. The columns of csv file
are: in_count, out_count, dwell_time.
Kindly help, I tried some guides about working with chart.js and django,
but no output came.
*Views.py file:*
class ChartData(APIView):
authentication_classes = []
permission_classes = []
def get(self, request, format=None):
data = {
"labels": ["In Count", "Out Count", "Time", "Average Dwell
Time"],
"data": [12, 19, 3, 2],
}
return Response(data)
User = get_user_model()
# class HomeView(View):
# def get(self, request, *args, **kwargs):
# return render(request, 'charts.html', {"customers": 10})
def get_data(request, *args, **kwargs):
data = {
"sales": 100,
"customers": 10,
}
return JsonResponse(data) # http response
class ChartData(APIView):
authentication_classes = []
permission_classes = []
def get(self, request, format=None):
qs_count = User.objects.all().count()
labels = ["Users", "Blue", "Yellow", "Green", "Purple", "Orange"]
default_items = [qs_count, 23, 2, 3, 12, 2]
data = {
"labels": labels,
"default": default_items,
}
return Response(data)
*Urls.py for chart:*
url(r'^api/chart/data/$', ChartData.as_view()),
url(r'^api/data/$', get_data, name='api-data'),
*Charts.html file:*
{% extends 'base.html' %}
<script>
{% block jquery %}
var endpoint = '/api/chart/data/'
var defaultData = []
var labels = [];
$.ajax({
method: "GET",
url: endpoint,
success: function(data){
labels = data.labels
defaultData = data.default
setChart()
},
error: function(error_data){
console.log("error")
console.log(error_data)
}
})
function setChart(){
var ctx = document.getElementById("myChart");
var ctx2 = document.getElementById("myChart2");
var myChart = new Chart(ctx2, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: '# of Votes',
data: defaultData,
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
}
}]
}
}
});
var myChart = new Chart(ctx, {
type: 'polarArea',
data: {
labels: labels,
datasets: [{
label: '# of Votes',
data: defaultData,
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
}
}]
}
}
});
}
// var ctx = document.getElementById("myChart");
{% endblock %}
</script>
{% block content %}
<div class='row'>
<div class='col-sm-12' url-endpoint='{% url "api-data" %}'>
<h1>Hello World</h1>
<div class='col-sm-6'>
<canvas id="myChart" width="400" height="400"></canvas>
</div>
<div class='col-sm-6'>
<canvas id="myChart2" width="400" height="400"></canvas>
</div>
</div>
</div>
{% endblock content %}
The content is as per the guide. The api is created and works fine but the
result chart does not get displayed in browser.
What i want is, the django app to check in its folder where the file will
be stored path to which shall be: app/static/app; to check and read the
latest file and plot a graph.
Kindly help me. I am completely clueless.
PS- I want the fields as in_count, out_count and dwell_time; you can tell
me the solution either taking reading from a csv file or a json file.
I shall be extremely thankful to you.
With Kind Regards
Aakash Baranwal
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.