简体   繁体   English

使用外部conf文件配置pygal图表?

[英]Configure a pygal chart with an external conf file?

I'm using pygal to chart some data in a web app I'm working on and thought it would be a good idea to externalize the configuration of the chart. 我正在使用pygal在正在处理的Web应用程序中绘制一些数据图表,并认为将图表的配置外部化是一个好主意。

So I wrote a section in my conf file that replicated my in code conf: 所以我在conf文件中写了一段,复制了我的代码conf:

[ChartOptions]
x_label_rotation: -75
x_labels_major_every: 5
show_minor_x_labels: False
range: (30,100)
stroke_style: {'width':8}
title: Chart Title

and found that passing the ChartOptions section to (for instance) pygal.Config() resulted in 并发现将ChartOptions部分传递到(例如)pygal.Config()会导致

  File "/usr/local/lib/python2.7/dist-packages/pygal/util.py", line 370, in mergextend
    if list1 is None or _ellipsis not in list1:

How can I do this? 我怎样才能做到这一点?

I'm fairly new at Python, so maybe this is all unnecessary, is well known or there exists a better way. 我是Python的新手,所以也许这一切都是不必要的,众所周知的或存在更好的方法。 I had a bunch of trouble and couldn't find anything, so here we are. 我遇到了很多麻烦,找不到任何东西,所以我们来了。

The first thing I worked out is that pygal.util.mergextend() does not like to find strings where it's expecting other datatypes. 我得出的第一件事是pygal.util.mergextend()不喜欢在期望其他数据类型的地方找到字符串。 The values in the OrderedDict returned from ConfigParser.read()._sections[your_section_here] are all strings, so they need to be converted to their proper type. 从ConfigParser.read()._ sections [your_section_here]返回的OrderedDict中的值都是字符串,因此需要将其转换为适当的类型。

Enter: ast.literal_eval(). 输入:ast.literal_eval()。

This seemed like it would work, but kept raising a ValueError('malformed string ') on the __name__ value, which was a str, per type(options[' __name__ ']). 这似乎可行,但是会在__name__值上按类型(options [' __name__ '])不断地引发ValueError('格式错误的字符串')。 Well, now what? 好吧,现在呢?

I didn't really need the __name__ value, so I used pop() to remove it from the dictionary, which left dealing with the title value. 我实际上并不需要__name__值,因此我使用pop()将其从字典中删除,剩下的就是处理title值。 I wanted to use title , knew could be a string per pygal and had control over the value of it, so what can be done? 我想使用title ,知道每个pygal可以是一个字符串,并且可以控制它的值,那么该怎么办?

The docs for ast.literal_eval() insist it allows strings, so adding quotes to the title value, in the conf file seemed 'reasonable', and worked. ast.literal_eval()的文档坚持认为它允许使用字符串,因此在conf文件中为title值添加引号似乎“合理”,并且可以正常工作。

Putting all that together, and adding flask to the mix, we get: 将所有内容放在一起,然后将烧瓶添加到混合物中,我们得到:

conf file: conf文件:

...
[ChartOptions]
x_label_rotation: -75
x_labels_major_every: 5
show_minor_x_labels: False
range: (30,100)
stroke_style: {'width':8}
# note added quotes below
title: "Chart Title"
...

app.py: app.py:

import ConfigParser
import ast
import pygal
from pygal import Config
from flask import Flask, render_template
...
config = ConfigParser.ConfigParser()
config.read('my.conf')
chart_options = config._sections[CHART_SECTION]
chart_options.pop('__name__')
for key in chart_options.keys():
    chart_options[key] = ast.literal_eval(chart_options[key])

@app.route('/route')
def a_route():
    global chart_options # haven't made this into a class yet...
    chart_config = Config(**chart_options)

    chart = pygal.Line(chart_config)
    ...
    # add data and finalize chart setup
    ...
    return render_template('route.html', chart=chart.render())
...

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

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