天天看点

python中pygal_Python pygal.Bar方法代码示例

# 需要导入模块: import pygal [as 别名]

# 或者: from pygal import Bar [as 别名]

def view_scores(cid, aid):

courses, current_course = get_courses(cid)

assign = Assignment.query.filter_by(id=aid, course_id=cid).one_or_none()

if not Assignment.can(assign, current_user, 'export'):

flash('Insufficient permissions', 'error')

return abort(401)

include_all = request.args.get('all', False, type=bool)

query = (Score.query.options(db.joinedload('backup'), db.joinedload(Score.grader))

.filter_by(assignment=assign))

if not include_all:

query = query.filter_by(archived=False)

# sort scores by submission time in descending order, to match front end display

query = query.order_by(Score.created.desc())

all_scores = query.all()

score_distribution = collections.defaultdict(list)

for score in all_scores:

score_distribution[score.kind].append(score.score)

bar_charts = collections.OrderedDict()

sorted_kinds = sorted(score_distribution, reverse=True,

key=lambda x: len(score_distribution[x]))

for kind in sorted_kinds:

score_values = score_distribution[kind]

score_counts = collections.Counter(score_values)

bar_chart = pygal.Bar(show_legend=False, x_labels_major_count=6, margin=0,

height=400, show_minor_x_labels=False, truncate_label=5)

bar_chart.fill = True

bar_chart.title = '{} distribution ({} items)'.format(kind, len(score_values))

bar_chart.add(kind, [score_counts.get(x) for x in sorted(score_counts)])

bar_chart.x_labels = [x for x in sorted(score_counts)]

bar_charts[kind] = bar_chart.render().decode("utf-8")

return render_template('staff/course/assignment/assignment.scores.html',

autograder_url=current_course.autograder_url,

assignment=assign, current_course=current_course,

courses=courses, scores=all_scores,

score_plots=bar_charts)