AngularJs中的Python脚本

问题描述 投票:-1回答:1

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

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

我无法弄清楚如何在Angular Js中集成这个python脚本。我怎样才能做到这一点?

python angularjs node.js
1个回答
3
投票

Python无法在浏览器中运行。所以你可以render your graph to pngoutput it to client using node

或者,有mpld3库几乎完全符合您的需要

© www.soinside.com 2019 - 2024. All rights reserved.