2017-12-18 104 views
-1

我是JavaScript新手,我正在研究一个将NodeJs作为服务器框架并将AngularJs作为应用程序框架的web应用程序。我想运行一个python脚本,在视图/模板上显示图形(matplotlib)。这是python代码。从mongodb服务器获取数据的过程可以在NodeJs控制器中完成。AngularJs中的Python脚本

# 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') 

我想不出如何将这个python脚本集成到Angular Js中。我怎样才能做到这一点?

回答