Table of Contents
回复内容:
Home Backend Development Python Tutorial 希望用flask作为中介让python和js交互,大家有什么比较好的实践经验没?

希望用flask作为中介让python和js交互,大家有什么比较好的实践经验没?

Jun 06, 2016 pm 04:23 PM
flask js pandas

我试过了getjson之类的是可以的但是比较怀疑数据量大的话用什么比较好啊?记得见过有人是pandas+d3.js+flask用的,不过找不到了

回复内容:

无邀自答,只因为在我主页的信息流里面多看了你一眼。。。。这话题选得各种切中我的要害啊。

我是勤劳的搬运工。


Getting Your Python Data Onto Browser介绍了如何将你的python数据弄到浏览器里面。所以其实JSON才是python和js的中介,Flask框架只是帮你简单的建立一个JSON服务器而已,连RESTfull都谈不上,如果你只是用来可视化的话。当然你也可以实现增修改删的RESTful服务。同理,你也可以用Tornado,也许它的性能更好。


我假定是一个动态的服务,那么Panda的DataFrame可以帮你快速地处理数据,Playing with REALTIME data, Python and D3和Data visualization using D3.js and Flask都有介绍相关的细节。


功能实现后,你所关心的可能是性能问题。我觉得大多数还不太会需要这方面的担忧。最烦人的可能是开发和可维护性。当然如果关心性能,你也可以在你功能完善的情况下,考虑服务器的优化,引入cache服务,服务器平衡等等,我觉得这不是你关心的。


此外,强烈推荐数据可视化控,可以试试ipython notebook. 这篇Visualizing a NetworkX graph in the IPython notebook with d3.js可能会让你强烈地喜欢上ipython notebook.

新手的个人经验:
比如现在我们要将一个数据从Javascript传输到Flask, 进行处理后再传回Javascript. 我们可以使用jQuery中的getJSON, 并将数据转化为String后进行传输.

Javascript + jQuery:
<span class="nx">mydata</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"msg"</span><span class="p">,</span> <span class="s2">"Hello Flask."</span><span class="p">};</span>          <span class="c1">// 要传输的数据</span>
<span class="nx">$</span><span class="p">.</span><span class="nx">getJSON</span><span class="p">(</span><span class="s1">'/dataconvector'</span><span class="p">,</span> <span class="p">{</span>              <span class="c1">// Flask中获取数据的function的url</span>
        <span class="nx">mykey</span><span class="o">:</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">mydata</span><span class="p">)</span>      <span class="c1">// 定义一个keyword, 将数据stringify</span>
    <span class="p">},</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span>                    <span class="c1">// 从Flask返回的数据</span>
        <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">result</span><span class="p">);</span>
        <span class="nx">$</span><span class="p">(</span> <span class="s2">"#result"</span> <span class="p">).</span><span class="nx">text</span><span class="p">(</span><span class="nx">data</span><span class="p">.</span><span class="nx">result</span><span class="p">);</span>
    <span class="p">}</span>
<span class="p">);</span>
Copy after login
websocket啊

另外推荐神器 GitHub - brython-dev/brython: Brython (Browser Python) is an implementation of Python 3 running in the browser

bottle-websocket flask-socketio, 相当简单易用
flask-socketio.readthedocs.org 入门上手的话推荐看这本:
Data Visualization with Python and JavaScript - O'Reilly Media 推荐一个Github上面的开源项目,Caravel(GitHub - airbnb/caravel: Caravel is a data exploration platform designed to be visual, intuitive, and interactive),我正在学习和研究它 在flask这个包里有jsonify这个方法,可以return这个方法的返回值,在js那边接受为json。

除此之外,还可以使用jinja过滤器,在html上将字典转化为js可以读取的格式。字符串就不必了。
参考我的博客
cnblogs.com/lewis617/p/ 用Flask搭建一个RESTful服务,以JSON作为数据接口,这样前台就能获取后台的数据。 tornado + websocket 也是一个选择,格式 json 比较方便。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solving common pandas installation problems: interpretation and solutions to installation errors Solving common pandas installation problems: interpretation and solutions to installation errors Feb 19, 2024 am 09:19 AM

Pandas installation tutorial: Analysis of common installation errors and their solutions, specific code examples are required Introduction: Pandas is a powerful data analysis tool that is widely used in data cleaning, data processing, and data visualization, so it is highly respected in the field of data science . However, due to environment configuration and dependency issues, you may encounter some difficulties and errors when installing pandas. This article will provide you with a pandas installation tutorial and analyze some common installation errors and their solutions. 1. Install pandas

Start from scratch and guide you step by step to install Flask and quickly establish a personal blog Start from scratch and guide you step by step to install Flask and quickly establish a personal blog Feb 19, 2024 pm 04:01 PM

Starting from scratch, I will teach you step by step how to install Flask and quickly build a personal blog. As a person who likes writing, it is very important to have a personal blog. As a lightweight Python Web framework, Flask can help us quickly build a simple and fully functional personal blog. In this article, I will start from scratch and teach you step by step how to install Flask and quickly build a personal blog. Step 1: Install Python and pip Before starting, we need to install Python and pi first

How to read txt file correctly using pandas How to read txt file correctly using pandas Jan 19, 2024 am 08:39 AM

How to use pandas to read txt files correctly requires specific code examples. Pandas is a widely used Python data analysis library. It can be used to process a variety of data types, including CSV files, Excel files, SQL databases, etc. At the same time, it can also be used to read text files, such as txt files. However, when reading txt files, we sometimes encounter some problems, such as encoding problems, delimiter problems, etc. This article will introduce how to read txt correctly using pandas

Django vs. Flask: A comparative analysis of Python web frameworks Django vs. Flask: A comparative analysis of Python web frameworks Jan 19, 2024 am 08:36 AM

Django and Flask are both leaders in Python Web frameworks, and they both have their own advantages and applicable scenarios. This article will conduct a comparative analysis of these two frameworks and provide specific code examples. Development Introduction Django is a full-featured Web framework, its main purpose is to quickly develop complex Web applications. Django provides many built-in functions, such as ORM (Object Relational Mapping), forms, authentication, management backend, etc. These features allow Django to handle large

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Practical tips for reading txt files using pandas Practical tips for reading txt files using pandas Jan 19, 2024 am 09:49 AM

Practical tips for reading txt files using pandas, specific code examples are required. In data analysis and data processing, txt files are a common data format. Using pandas to read txt files allows for fast and convenient data processing. This article will introduce several practical techniques to help you better use pandas to read txt files, along with specific code examples. Reading txt files with delimiters When using pandas to read txt files with delimiters, you can use read_c

Guide to installing the Flask framework: Detailed steps to help you install Flask correctly Guide to installing the Flask framework: Detailed steps to help you install Flask correctly Feb 18, 2024 pm 10:51 PM

Flask framework installation tutorial: Teach you step by step how to correctly install the Flask framework. Specific code examples are required. Introduction: Flask is a simple and flexible Python Web development framework. It's easy to learn, easy to use, and packed with powerful features. This article will lead you step by step to correctly install the Flask framework and provide detailed code examples for reference. Step 1: Install Python Before installing the Flask framework, you first need to make sure that Python is installed on your computer. You can start from P

Revealing the efficient data deduplication method in Pandas: Tips for quickly removing duplicate data Revealing the efficient data deduplication method in Pandas: Tips for quickly removing duplicate data Jan 24, 2024 am 08:12 AM

The secret of Pandas deduplication method: a fast and efficient way to deduplicate data, which requires specific code examples. In the process of data analysis and processing, duplication in the data is often encountered. Duplicate data may mislead the analysis results, so deduplication is a very important step. Pandas, a powerful data processing library, provides a variety of methods to achieve data deduplication. This article will introduce some commonly used deduplication methods, and attach specific code examples. The most common case of deduplication based on a single column is based on whether the value of a certain column is duplicated.

See all articles