使用Python生成JSON数据
我正在开发一个小项目,目的是从服务器的数据库生成报告。数据库是 SQLite,包含“连接数”、“下载数”等表格。
我生成的报告最终将包含许多图表,显示“每日连接数”、“本月热门下载数”等内容。
我计划使用 flot 作为图表,因为它制作的图表看起来非常漂亮:
这是我当前的报告工作方式计划:
静态 .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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这对我来说听起来很明智。
It sounds sensible to me.
也许是这样,但即便如此,Python 仍然是一种很棒的粘合语言。另外,如果你需要做一些 SQLite 不擅长的处理,Python 已经在那里了。
我认为您倾向于一般类型的 HTTP 服务报告。与您的问题集重叠的一件事是 Django,它提供了数据库之间的 Python 接口(支持 SQLite)和网络服务器,以及用于输出的模板系统。
如果您只需要一两个解决方案,那么我建议您查看 SQLAlchemy 用于与数据库交互,< a href="http://jinja.pocoo.org" rel="nofollow">Jinja2 用于模板,和/或 Werkzeug 用于 HTTP 服务器接口。
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.
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.