在Django服务器中运行复杂的计算(使用python/pandas) [英] Running complex calculations (using python/pandas) in a Django server

查看:337
本文介绍了在Django服务器中运行复杂的计算(使用python/pandas)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用python中的Django-rest-framework开发了RESTful API.我开发了所需的模型,对其进行了序列化,设置了令牌身份验证以及随之而来的所有其他尽职调查.

I have developed a RESTful API using the Django-rest-framework in python. I developed the required models, serialised them, set up token authentication and all the other due diligence that goes along with it.

我还使用Angular构建了一个前端,该前端托管在另一个域中.我设置了CORS修改,因此可以根据需要访问API.一切似乎都正常.

I also built a front-end using Angular, hosted on a different domain. I setup CORS modifications so I can access the API as required. Everything seems to be working fine.

这是问题所在.我正在构建的Web应用程序是一个财务应用程序,应该允许用户在服务器上运行一些复杂的计算并将结果发送到前端应用程序,以便可以将它们呈现为图表和其他格式.我不知道如何或在何处进行这些计算.

Here is the problem. The web app I am building is a financial application that should allow the user to run some complex calculations on the server and send the results to the front-end app so they can be rendered into charts and other formats. I do not know how or where to put these calculations.

我选择Django作为后端,因为我期望python可以在需要的地方帮助我运行此类计算.基本上,当我在服务器上调用特定的api链接时,我希望能够从数据库中检索多个表中的数据(如果需要),并使用该数据通过python或python库(pandas或numpy)运行某些计算),并将计算结果作为对API调用的响应.

I chose Django for the back-end as I expected that python would help me run such calculations wherever required. Basically, when I call a particular api link on the server, I want to be able to retrieve data from my database, from multiple tables if required, and use the data to run some calculations using python or a library of python (pandas or numpy) and serve the results of the calculations as response to the API call.

如果这是一项艰巨的任务,我至少希望能够使用API​​从表中检索数据到前端,使用JS处理数据,然后将其发送到位于具有处理过的数据的服务器,此功能将运行必要的复杂计算并响应结果,并将结果呈现为图表/其他格式.

If this is a daunting task, I at least want to be able to use the API to retrieve data from the tables to the front-end, process the data a little using JS, and send it to a python function located on the server with this processed data, and this function would run the necessary complex calculations and respond with results which would be rendered into charts / other formats.

任何人都可以指出我从这里离开的方向吗?我在网上寻找资源,但我想找不到正确的关键字来搜索它们.我只希望将某种外壳程序代码集成到当前支持的程序中,从而可以调用我编写的一些python脚本来运行这些计算.

Can anyone point me to a direction to move from here? I looked for resources online but I think I am unable to find the correct keywords to search for them. I just want a shell code kind of a thing to integrate into my current backed using which I can call some python scripts that I write to run these calculations.

谢谢.

推荐答案

我假设您的问题是如何在django的宁静框架中进行这些计算?",但我认为在这种情况下,您需要离开从那个想法开始.

I assume your question is about "how do I do these calculations in the restful framework for django?", but I think in this case you need to move away from that idea.

您正确地完成了所有操作,但是RESTful API提供了资源-基本上是您的模型.

You did everything correctly but RESTful APIs serve resources -- basically your model.

但是,计算并非如此.如我所见,您可以通过两种方式实现所需的目标:

A computation however is nothing like that. As I see it, you have two ways of achieving what you want:

1)编写一个表示计算结果并使用RESTful框架提供服务的模型,因此您的计算是一种资源(如果将结果存储在数据库中作为缓存的方式可以很好地工作)

1) Write a model that represents the results of a computation and is served using the RESTful framework, thus your computation being a resource (can work nicely if you store the results in your database as a way of caching)

2)向您的api添加一个路由/端点,该路由/端点用于提供该计算的结果.

2) Add a route/endpoint to your api, that is meant to serve results of that computation.

路径1:以资源计算

创建一个模型,该模型在实例化时处理计算. 您甚至可以为计算建立继承结构,并为计算模型实现接口.

Create a model, that handles the computation upon instantiation. You could even set up an inheritance structure for computations and implement an interface for your computation models.

这样,当请求资源并且restful框架想要提供该资源时,将提供计算结果.

This way, when the resource is requested and the restful framework wants to serve this resource, the computational result will be served.

路径2:自定义端点

为计算端点(如/myapi/v1/taxes/compute)添加一条路由.

Add a route for your computation endpoints like /myapi/v1/taxes/compute.

在此终结点的基础控制器中,您将加载计算所需的模型,执行计算,并随心所欲地提供结果(可能是json响应).

In the underlying controller of this endpoint, you will load up the models you need for your computation, perform the computation, and serve the result however you like it (probably a json response).

您仍然可以使用上述继承结构来实现计算.这样,您可以基于参数实例化Computation对象(在上述情况下为taxes).

You can still implement computations with the above mentioned inheritance structure. That way, you can instantiate the Computation object based on a parameter (in the above case taxes).

这能给您一个主意吗?

这篇关于在Django服务器中运行复杂的计算(使用python/pandas)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆