使用Python生成JSON数据

发布于 2024-10-16 00:57:18 字数 910 浏览 1 评论 0原文

我正在开发一个小项目,目的是从服务器的数据库生成报告。数据库是 SQLite,包含“连接数”、“下载数”等表格。

我生成的报告最终将包含许多图表,显示“每日连接数”、“本月热门下载数”等内容。

我计划使用 flot 作为图表,因为它制作的图表看起来非常漂亮:flot graph

这是我当前的报告工作方式计划:

  • 静态 .HTML 文件,报告。这将包含标题、嵌入式图表等。

  • JSON 数据文件。这些将由我的报告生成 python 脚本生成,它们基本上包含每个图形的 JSON 变量,表示图形应映射的数据集。 ([100,2009-2-2],[192,2009-2-3]...)

  • 报告生成 python 脚本,这将加载 SQLite 数据库,运行一组 SQL 查询并吐出JSON 数据文件。

这听起来是一个明智的设置吗?我忍不住觉得它可以改进,但我不知道如何改进。我希望报告是静态的。它们运行的​​服务器无法承受重负载,因此动态生成的报告是不可能的,并且对于该应用程序来说也是不必要的。

我的担忧是:

  • 我觉得 Python 脚本基本上没有意义,所有执行的处理都是由 SQLite 完成的,我的脚本基本上将用于存储 SQL 查询并打包输出。如果再做一点工作,SQLite 可能可以为我做到这一点。

  • 看来我正在解决一个问题,这个问题在“进行 SQL 查询,在每日报告中吐出漂亮的图表”之前必须已经解决过很多次了。我只是在追踪任何广泛的实现方面遇到了麻烦。

I'm working on a little project, the aim being to generate a report from a database for a server. The database is SQLite and contains tables like 'connections', 'downloads', etc.

The report I produce will ultimately contain a number of graphs displaying things like 'connections per day', 'top downloads this month', etc.

I plan to use flot for the graphs because the graphs it makes look very nice:flot graph

This is my current plan for how my reports will work:

  • Static .HTML file which is the report. This will contain headings, embedded flot graphs, etc.

  • JSON Data file. These will be generated by my report generation python script, they will basically contain a JSON variable for each graph representing the dataset that the graph should map. ([100,2009-2-2],[192,2009-2-3]...)

  • Report generation python script, this will load the SQLite database, run a list of set SQL queries and spit out the JSON Data files.

Does this sound like a sensible set up? I can't help but feel it could be improved but I don't see how. I want the reports to be static. The server they run on cannot take heavy loads so a dynamically generated report is out of the question and also unnecessary for this application.

My concerns are:

  • I feel that the Python script is largely pointless, all of the processing performed is done by SQLite, my script is basically going to be used to store SQL queries and package up the output. With a bit more work SQLite could probably do this for me.

  • It seems I'm solving a problem that must have been solved many times before 'take sql queries, spit out pretty graphs in a daily report' must have been done hundreds of times. I'm just having trouble tracking down any broad implementations.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光下的泡沫是彩色的 2024-10-23 00:57:18

这对我来说听起来很明智。

  • 您需要一些编程语言来与 SQLite 对话。您可以用 C 语言完成,但如果您可以用 Python 轻松编写必要的粘合代码,为什么不呢?几乎可以肯定,与没有最有效的程序而损失的时间相比,您编写它会节省更多的时间。
  • 肯定有一些程序可以为您分析日志 - 例如,我听说过 Piwik。这是针对动态报告的,但毫无疑问也有一些项目可以做静态报告。但它们可能不适合您想要的数据和输出。自己编写意味着您确切地知道自己会得到什么,所以如果工作量不大,就继续吧。

It sounds sensible to me.

  • You need some programming language to talk to SQLite. You could do it in C, but if you can write the necessary glue code easily in Python, why not? You'll almost certainly save more time writing it than you'll lose from not having the most efficient possible program.
  • There are definitely programs to analyse logs for you - I've heard of Piwik, for instance. That's for dynamic reports, but no doubt there are projects to do static reports too. But they may not fit the data and output you're after. Writing it yourself means you know precisely what you're getting, so if it's not too much work, carry on.
疾风者 2024-10-23 00:57:18

我觉得Python脚本基本上没有意义,所有执行的处理都是由SQLite完成的,我的脚本基本上将用于存储SQL查询并打包输出。通过更多的工作,SQLite 可能可以为我做到这一点。

也许是这样,但即便如此,Python 仍然是一种很棒的粘合语言。另外,如果你需要做一些 SQLite 不擅长的处理,Python 已经在那里了。

看来我正在解决一个问题,这个问题在“进行 SQL 查询,在每日报告中吐出漂亮的图表”之前必须已经解决过很多次了。我只是无法追踪任何广泛的实现。

我认为您倾向于一般类型的 HTTP 服务报告。与您的问题集重叠的一件事是 Django,它提供了数据库之间的 Python 接口(支持 SQLite)和网络服务器,以及用于输出的模板系统。

如果您只需要一两个解决方案,那么我建议您查看 SQLAlchemy 用于与数据库交互,< a href="http://jinja.pocoo.org" rel="nofollow">Jinja2 用于模板,和/或 Werkzeug 用于 HTTP 服务器接口。

I feel that the Python script is largely pointless, all of the processing performed is done by SQLite, my script is basically going to be used to store SQL queries and package up the output. With a bit more work SQLite could probably do this for me.

Maybe so, but even then, Python is a great glue language. Also, if you need to do some processing SQLite isn't good at, Python is already there.

It seems I'm solving a problem that must have been solved many times before 'take sql queries, spit out pretty graphs in a daily report' must have been done hundreds of times. I'm just having trouble tracking down any broad implementations.

I think you're leaning towards the general class of HTTP-served reporting. One thing out there that overlaps your problem set is Django, which provides a Python interface between database (SQLite is supported) and web server, along with a templating system for your outputs.

If you just want one or two pieces of a solution, then I recommend looking at SQLAlchemy for interfacing with the database, Jinja2 for templating, and/or Werkzeug for HTTP server interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文