-1

I am new to JavaScript, I am working on a web-app which has NodeJs as server-framework and AngularJs as application framework. I would like to run a python script which displays graphs(matplotlib) on the view/template. Here is the python code. The process of fetching data from mongodb server can be done in NodeJs controller.

# In[2]:

import pymongo
import json
import datetime as dt
from pymongo import MongoClient
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt
import numpy as np


# In[77]:

l=['ts','cd']
df = pd.DataFrame(columns=l)
k=0
if __name__ == '__main__':
    client = MongoClient("localhost", 27017, maxPoolSize=50)
    db=client.test
    collection=db['data']
    cursor = collection.find({"dId":3},{"ts":1,"cd":1}).sort("ts",-1).limit(25000)
    for document in cursor:
        df.loc[k,'ts']=document.values()[1]
        df.loc[k,'cd']=document.values()[2]
        k=k+1


# In[78]:

times = pd.DataFrame(pd.to_datetime(df.ts))    

# In[79]:

times['hour']=times['ts'].dt.hour
times['date']=times['ts'].dt.date
times['weekday']=times['ts'].dt.weekday


# In[80]:

grouped = pd.DataFrame(columns=['date','hour','weekday','count'])
grouped[['date','hour','weekday','count']] = times.groupby(['date','hour','weekday']).count().reset_index()


# In[81]:

irregularities=grouped[grouped['count'] != 60][['date','hour','weekday','count']]


# In[82]:

get_ipython().magic(u'matplotlib inline')
x = irregularities['weekday']
plt.hist(x, bins=30)
plt.ylabel('Count of irregularities')
plt.xlabel('day of week')
plt.xticks([0,1,2,3,4,5,6],['mon','tue','wed','thurs','fri','sat','sun'])


# In[86]:

x = irregularities['hour']
plt.hist(x, bins=30)
plt.ylabel('Count of irregularities')
plt.xlabel('Hour of day')

I can't figure out how to integrate this python script in Angular Js. How can I do that?

Saksham Bassi
  • 65
  • 1
  • 9

1 Answers1

3

Python can't be run in browser. So you can render your graph to png and output it to client using node.

Alternatively there is mpld3 library that does almost exactly what you need

iofjuupasli
  • 3,818
  • 1
  • 16
  • 17