桥接Python后端和JavaScript前端 [英] Bridging a Python back-end and JavaScript front-end

查看:74
本文介绍了桥接Python后端和JavaScript前端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从事一个项目,该项目涉及通过用户提供的文件进行解析,使用该数据进行计算以及使用图形工具将结果可视化.现在,我坚持使用Python作为后端,因为它具有JavaScript中不可用的科学库,但是我想将整个工具移到Web服务器上,在这里我可以使用D3.js进行很多更流畅的可视化./p>

工作流程类似于:从浏览器获取文件内容,使用内容执行Python脚本,返回计算值的jsonified对象,并使用D3绘制这些对象.我已经有后端和前端在自己工作,但是想知道:我如何才能将两者联系起来?从我收集的数据来看,我需要在整个过程中做一些事情.启动服务器,向服务器发送AJAX请求以及从服务器检索数据的几行.但是,由于存在许多框架(Flask,cgi,apache,websocket等),我不太确定从哪里开始.这可能只是一个非常简单的Web应用程序,只有一个文件提交页面和一个数据可视化页面.感谢您的帮助!

解决方案

Apache是​​Web服务器,flask是python中的Web框架,websockets是协议,而cgi是完全不同的东西,它们都不会对您有所帮助前端.

您可以在flask或django或pylon或任何其他python Web框架中部署一个简单的后端.我喜欢django,但对于您的用途来说可能有点重,flask更轻巧.您将它们部署到安装了Web服务器的服务器上,并使用apache之类的东西进行分发.

然后,您需要一个前端以及一种交付前端的方法.Flask/Django都具有与Web服务器结合使用的全部功能,或者您可以使用像Amazon S3这样的静态文件服务器.

在您的前端,您需要加载D3以及jQuery之类的实用程序,以便从后端加载和解析数据,然后使用D3,但您希望将其显示在屏幕上.

I'm currently working on a project that involves parsing through a user-supplied file, doing computations with that data, and visualizing the results using graphing utilities. Right now, I'm stuck with using Python as the back-end because it has scientific libraries unavailable in JavaScript, but I want to move the entire tool to a web server, where I can do much slicker visualizations using D3.js.

The workflow would be something like: obtain the file contents from the browser, execute the Python script with the contents, return jsonified objects of computed values, and plot those objects using D3. I already have the back-end and front-end working by themselves, but want to know: How can I go about bridging the two? From what I've gathered, I need to do something along the lines of launching a server, sending AJAX requests to the server, and retrieving data from the server. But with the number of frameworks out there (Flask, cgi, apache, websockets, etc.), I'm not really sure where to start. This will likely only be a very simple web app with just a file submit page and a data visualization page. Any help is appreciated!

解决方案

Apache is a web server, flask is a web framework in python, websockets are a protocol, and cgi is something totally different, and none of them help you on the front end.

You could deploy a simple backend in flask or django or pylons or any other python web framework. I like django, but it may be a little heavy for your purpose, flask is a bit more lightweight. You deploy them to a server with a web server installed and use something like apache to distribute.

Then you need a front end and a way of delivering your front end. Flask / Django are both fully capable of doing so in conjunction with a web server, or you could use a static file server like Amazon S3.

On your front end, you need to load D3 and probably some kind of utility like jQuery to load and parse your data from the back end, then use D3 however you like to present it on screen.

这篇关于桥接Python后端和JavaScript前端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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