简体   繁体   English

python 从 mysql 中绘制数据。 最好的方法?

[英]python charting data from mysql. Best approach?

I'm building a web app that shows the number of enquiries in a given date range.我正在构建一个网络应用程序,显示给定日期范围内的查询数量。 For example, the user will select "Last 14 days" and it will display a chart of the enquiries例如,用户将选择“过去 14 天”,它会显示查询图表

The chart will then have to update itself as the enquiries grow that day.随着当天查询量的增加,图表将不得不自行更新。

The data is stored in MySQL on a database server.数据存储在数据库服务器上的 MySQL 中。 The web app is written in python. Web 应用程序是用 python 编写的。

What would be the best approach to tackle this in your opinion?在您看来,解决这个问题的最佳方法是什么? with two questions in mind:有两个问题:

  1. Would you handle the querying the data on the SQL side?你会处理在 SQL 端查询数据吗? (so python calls a specific mysql query based on the user input - SELECT * from TABLE where date = x (where x = user input for example) or would you download the whole table and do the manipulation within python? Or a combination of both given that number of enquiries for all previous days won't change after that day has finished? (因此,python 根据用户输入调用特定的 mysql 查询 - SELECT * from TABLE where date = x (例如,其中 x = 用户输入)还是您会下载整个表并在 python 中进行操作?或者两者的组合鉴于当天结束后所有前几天的查询数量不会改变?

  2. Would you set a loop in python to download the database every 5mins or so, check for changes and update the chart?您会在 python 中设置一个循环,每 5 分钟左右下载一次数据库,检查更改并更新图表吗? or handle it another way?或以其他方式处理?

If I do understand your questions correctly, you want advice about what would be the "best" approach to your problem.如果我确实正确理解了您的问题,您需要有关解决问题的“最佳”方法的建议。

As we all know there is no best way, but only solution that fit well in solving a give problem.众所周知,没有最好的方法,只有适合解决给定问题的解决方案。

  1. I would avoid loading all the data, because it will become a bottle neck in your application as the amount of data will grow.我会避免加载所有数据,因为随着数据量的增长,它会成为您应用程序中的瓶颈。 Querying the database with your date parameters gather from a web form is the right way to go.使用从 Web 表单收集的日期参数查询数据库是正确的方法。 By the way since you seems to need these to lives for some times as your inquiries output could be consider à database report, you may consider store the parameters the user input with a name for the report in a table, so your users can retrieve these report without having to input the parameters again.顺便说一下,由于您的查询输出可能是数据库报告,因此您似乎需要这些数据存在一段时间,您可以考虑将用户输入的参数与报告名称一起存储在表中,以便您的用户可以检索这些无需再次输入参数即可报告。 Also, you seems to ignore what an ORM ( http://www.sqlalchemy.org/ ) or a DAL ( https://github.com/web2py/pydal ) are.此外,您似乎忽略了 ORM ( http://www.sqlalchemy.org/ ) 或 DAL ( https://github.com/web2py/pydal ) 是什么。 I would suggest you to use such a kind of tool to access your database instead of writing SQL query.我建议您使用这种工具来访问您的数据库,而不是编写 SQL 查询。

  2. Why don't you just tell your user to press F5 or CTRL+R ?为什么不直接告诉用户按F5CTRL+R In most browser these command will make the page reload and since the parameters to generate your report, which should be present in the URL, the report will be regenerate with the new inserted data available into the database.在大多数浏览器中,这些命令将使页面重新加载,并且由于生成报告的参数应该存在于 URL 中,报告将使用新插入的数据重新生成到数据库中。

If this solution is to old school, I will suggest you to read about "Long Polling", WebSocket, etc.如果这个解决方案是老派,我建议你阅读“长轮询”、WebSocket 等。

Here a SO question about that with a good answer : What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?这里有一个关于这个问题的很好的答案: 什么是长轮询、Websockets、服务器发送事件 (SSE) 和 Comet?

This represent much more work and skill to master than simply the regular HTTP, that why I suggest you to start with the basic...这意味着要掌握的工作和技能比简单的常规 HTTP 要多得多,这就是为什么我建议您从基本...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM