简体   繁体   English

使用Python生成JSON数据

[英]Using Python to generate JSON Data

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. 该数据库是SQLite,包含“连接”,“下载”等表。

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用于图形,因为它使图形看起来非常漂亮: 浮点图

This is my current plan for how my reports will work: 这是我目前的报告工作计划:

  • Static .HTML file which is the report. 静态.HTML文件, 报告。 This will contain headings, embedded flot graphs, etc. 这将包含标题,嵌入式浮点图等。

  • JSON Data file. JSON数据文件。 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. 这些将由我的报告生成python脚本生成,它们将基本上为每个表示每个图的图包含一个JSON变量,表示该图应映射的数据集。 ([100,2009-2-2],[192,2009-2-3]...) ([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. 报告生成python脚本,它将加载SQLite数据库,运行一组已设置的SQL查询并吐出JSON数据文件。

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. 我觉得Python脚本基本上没有意义,所有执行的处理都是由SQLite完成的,我的脚本基本上将用于存储SQL查询和打包输出。 With a bit more work SQLite could probably do this for me. 通过更多的工作,SQLite可能可以为我完成此工作。

  • 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. 看来我正在解决一个问题,在“执行sql查询,在每日报告中吐出漂亮的图形”必须完成数百次之前,必须解决许多次。 I'm just having trouble tracking down any broad implementations. 我只是在跟踪任何广泛的实现方面遇到麻烦。

It sounds sensible to me. 对我来说听起来很明智。

  • You need some programming language to talk to SQLite. 您需要一些编程语言才能与SQLite对话。 You could do it in C, but if you can write the necessary glue code easily in Python, why not? 您可以用C做到这一点,但是如果您可以用Python轻松编写必要的粘合代码,那为什么不呢? 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. 肯定有一些程序可以为您分析日志-例如,我听说过Piwik 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. 自己编写它意味着您确切地知道自己所得到的,因此,如果工作量不太多,请继续。

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. 我觉得Python脚本基本上没有意义,所有执行的处理都是由SQLite完成的,我的脚本基本上将用于存储SQL查询和打包输出。 With a bit more work SQLite could probably do this for me. 通过更多的工作,SQLite可能可以为我完成此工作。

Maybe so, but even then, Python is a great glue language. 也许是这样,但是即使那样,Python还是一种很棒的粘合语言。 Also, if you need to do some processing SQLite isn't good at, Python is already there. 另外,如果您需要做一些SQLite不擅长的处理,则Python已经存在。

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. 看来我正在解决一个问题,在“执行sql查询,在每日报告中吐出漂亮的图形”必须完成数百次之前,必须解决许多次。 I'm just having trouble tracking down any broad implementations. 我只是在跟踪任何广泛的实现方面遇到麻烦。

I think you're leaning towards the general class of HTTP-served reporting. 我认为您倾向于使用HTTP提供的报告的通用类。 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. 与问题集重叠的一件事是Django ,它提供了数据库(支持SQLite)和Web服务器之间的Python接口,以及用于输出的模板系统。

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. 如果您只需要一个或两个解决方案,那么我建议您查看SQLAlchemy与数据库的接口, Jinja2作为模板的模板和/或Werkzeug的HTTP服务器接口。

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

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