2017-12-18 84 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中。我怎樣才能做到這一點?

回答