A tutorial on the implementation of data visualization in Python's tornado framework

Source: Internet
Author: User
Tags date1 datetime join

This article mainly introduces examples of Python Tornado framework to achieve data visualization of the tutorial, Tornado is an asynchronous development framework for high man, the need for friends can refer to the

Expansion module used

XLRD:

In the Python language, read the extension tool for Excel. You can implement the specified form, read the specified cell.

Must be installed before use.

Download Address: https://pypi.python.org/pypi/xlrd

After decompression CD to extract directory, execute python setup.py install can

Datetime:

Python's built-in module for manipulating date time

To implement functional modules

Read XLS file and input database

According to the year, month, day three parameters to get the day on duty

Pie chart (number of days on duty/day not completed on duty)

Waterfall Chart (on duty of all on duty on the day)

According to the year, the month two parameter obtains the month the duty situation

According to the annual parameter to obtain the current year's duty situation

Duty system

There are 6 classes a day:

8:00-9:45

9:45-11:20

13:30-15:10

15:10-17:00

17:00-18:35

19:00-22:00

Everyone is worth one shift a day.

Only on duty time and half hour before and after the clock is valid.

Work, work must be clocked, missing clocking is considered not on duty.

Analyze Excel Tables

My fingerprint attendance machine can export up to one months of clocking out at a time. One problem is that these one months may span two months, or they may span a year. For example: March 21, 2015-April 20, 2015, December 15, 2014-January 05, 2015. So write the processing method must pay attention to this pit.

The exported table is shown in the following illustration:

= = It looks as if there is no one on duty, yes, that's it.

Everyone is so lazy t. T

Sign ...

With a simple analysis,

The attendance record is the third sheet of the file.

Third line has start and end time

Line four is the number of all dates

Next every two lines: first behavior user information; second ACT attendance record

Ideas

decided to store the relevant information separately with 3 collection:

User: information, including ID, name, dept

Record: attendance records, including ID (user ID), y (year), M (month), D (Day), check (punch record)

Duty: On-duty arrangement, including ID (number of weeks, example: 1 for Monday), list (on duty personnel ID), user_id:["Start_time", "End_time"] (user duty start time and end time)

Read XLS files, save new attendance records and new users to the database.

According to the date of the query corresponding to the record, query the day of the schedule, matching the day on duty students attendance records. will be on duty schoolmate's clocking time and the duty time comparison pair, determines whether the normal clocks out, calculates the actual duty time long, the actual duty percentage.

The JSON format data is then output and the graph is generated using echarts.

Analysis of the month, the year's attendance record the same, but may be a little more complicated.

All the explanations and specific ideas are placed in the source code comments, please continue to look down the source code

Source

main.py

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 #!/usr/bin/env python #-*-coding:utf-8-*-import os.path   Import tornado.auth import Tornado.escape import torn Ado.httpserver Import tornado.ioloop Import tornado.options import tornado.web from tornado.options import define, option S   Import Pymongo import time import datetime import xlrd   define ("Port", default=8007, help= "run on" given Port ", Type=int)   class application (tornado.web.Application): def __init__ (self): handlers = [(R"/", MainHandler), (r "/read", Readhandler), (R "/day", Dayhandler),] "settings = Dict" (Template_path=os.path.join (Os.path.dirname __file__ ), "Templates"), Static_path=os.path.join (Os.path.dirname (__file__), "Static"), DEBUG=TRUE, conn = Pymongo. Connection ("localhost", 27017) self.db = conn["Kaoqin"] tornado.web.application.__init__ (self, Handlers, **settings) & nbsp   Class MainHandler (Tornado.web.RequestHandler): Def get (self): Pass   class Readhandler ( Tornado.web.RequestHandler): Def get (self): #获取collection Coll_record = Self.application.db.record Coll_user = self.application.db.user #读取excel表格 table = Xlrd.open_workbook ('/users /ant/webdev/python/excel/data.xls ') #读取打卡记录sheet Sheet=table.sheet_by_index (2) #读取打卡月份范围 row3 = sheet.row_values (2) m1 = Int (Row3[2][5:7]) m2 = int (row3[2][18:20]) #设置当前年份 y = Int (row3[2][0:4)) #设置当前月份为第一个月份 m = M1 #读取打卡日期范围 row4 = sheet. Row_values (3) #初始化上一天 lastday = row4[0] #遍历第四行中的日期 for D in Row4: #如果日期小于上一个日期 #说明月份增大, modify the current month to the second month if D < lastday:m = M2 #如果当前两个月份分别为12月和1月 #说明跨年了, so the year +1 if M1 = = M2 = = 1:y = y + 1 #用n计数, range 3 to (total number/2+1) # (row number/2+1)-3 = number of users #即遍历 All users for N in range (3, sheet.nrows/2+1): #取该用户的第一行, that is, the user information line row_1 = Sheet.row_values (n*2-2) #获取用户id u_id = row_1[2] #获取用户姓名 U_name = row_1[10] #获取用户部门 u_dept = row_1[20] #查询该用户 user = Coll_user.find_one ({"id": u_id}) #如果数据库中不存在该用户则创建新用户 if not use R:user = Dict () user[' id ' = u_id user[' name '] = u_name user[' dept '] = u_dept coll_user.insert (user) #取该用户的第二行, that is, the absence record line row _2 = Sheet.row_values (n*2-1) #获取改Subscript idx = Row4.index (d) #获取当前用户当前日期的考勤记录 check_data = row_2[idx] #初始化空考勤记录列表 check = list () #5个字符一组, traverse attendance record and deposit attendance record list fo R i in range (0,len (check_data)/5): Check.append (check_data[i*5:i*5+5]) #查询当前用户当天记录 The record = Coll_record.find_one ({"Y": Y, "M": M, "D":d, "id": user[' id ']}) #如果记录存在则更新记录 if Record:for item in check: #将新的考勤记录添加进之前的记录 If Item is not in record[' Chec K ']: record[' Check '].append (item) Coll_record.save (record) #如果记录不存在则插入新纪录 Else:record = {"Y": Y, "M": M, "D":d, "id": User [' ID '], ' Check ': check} coll_record.insert (record)

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111-112 Class Dayhandler (Tornado.web.RequestHandler): Def get (self): #获取年月日参数 y = self.get_argument ("y", None) m = Self.get_argum ENT ("M", none) d = self.get_argument ("D", none) #判断参数是否设置齐全 if Y and M and D: #将参数转换为整型数, easy to use y = Int (y) m = int (m) d = Int ( d) #获取当天所有记录 Coll_record = Self.application.db.record record = Coll_record.find ({"Y": Y, "M": M, "D":d}) #获取当天为星期几 weekday = Datetime.datetime (y,m,d). Strftime ("%w") #获取当天值班表 coll_duty = self.application.db.duty duty = Coll_duty.find_one ({"id" : Int (Weekday)}) #初始化空目标记录 (day on duty) target = list () #遍历当天所有记录 for item in record: #当该记录的用户当天有值班任务时, calculated and deposited in the target array if int (it em[' id '] in duty[' list ': #通过用户id获取该用户值班起止时间 start = duty[item[' id ']][0] end = duty[item[' id ']][1] #计算值班时长/sec date1 = Date Time.datetime (Y,m,d,int (start[:2)), int (start[-2:]) Date2 = Datetime.datetime (Y,m,d,int (end[:2)), int (end[-2:]) item[' length '] = (date2-date1). Seconds #初始化实际值班百分比 item[' per ' = 0 #初始化上下班打卡时间 item[' start ' = 0 item[' End ' = 0 #遍历该用户打 Card record for T in item[' check ']: #当比值班时间来得早If T < start: #计算时间差 date1 = Datetime.datetime (Y,m,d,int (start[:2)), int (start[-2:]) Date2 = Datetime.datetime (Y,m,d, Int (t[:2]), int (t[-2:])) dif = (date1-date2). Seconds #当打卡时间在值班时间前半小时内 If dif <= 1800: #上班打卡成功 item[' start ' = Start El If T < end: #如果还没上班打卡 If isn't item[' start ': #则记录当前时间为上班打卡时间 item[' start ' = T else: #否则记录当前时间为下班打卡时间 item[' end ' = t El SE: #如果已经上班打卡 if item[' start ': #计算时间差 date1 = Datetime.datetime (Y,m,d,int (end[:2)), int (end[-2:)) Date2 = Datetime.datetime (Y,m,d,int (t[:2)), int (t[-2:]) dif = (date1-date2). Seconds #当打卡时间在值班时间后半小时内 If dif <= 1800: #下班打卡成功 item[' end '] = end #当上班下班均打卡 If item[' start '] and item[' End ': #计算实际值班时长 date1 = Datetime.datetime (Y,m,d,int (item[' start ') [: 2]), int (item[' start '][-2:])) Date2 = Datetime.datetime (y,m,d,int ' End ' item[]), int (][:2 ' End ' item[:])][-2 = ( date2-date1). Seconds #计算 (actual on-duty long/on-duty length) percent item[' per ' = Int (dif/float (item[' length ')) Else: #未正常上下班则视为未值班 item[' s Tart '] = 0 item[' End ' = 0 #将记录添加到target数组中 target.Append (item) #输出数据 Self.render ("index.html", target = target)     def main (): Tornado.options.parse_command_li NE () http_server = Tornado.httpserver.HTTPServer (Application ()) Http_server.listen (Options.port) Tornado.ioloop.IOLoop.instance (). Start ()     If __name__ = "__main__": Main ()   index.html   {% for Item in target%} {' id ': {{item[' id ']}}, ' Start ': {{item[' start '}}, ' End ': {{item[' end '}}, ' length ': {{item[' length ']}, ' per ': {{item['/}}} {% end%}}

At last

For the time being, write only to read the file and inquire about the day on duty, then will continue to follow the previous plan to write this small application finished.

Because it involves a bunch of small partners privacy, so did not send the test file. But if you want to actually run a look at the students can tell me, I sent you the document.

Possible use of a database INSERT statement: Db.duty.insert ({"id": 5, "list": [1,2],1:["19:00", "22:00"],2:["19:00", "22:00"]})

Hope to be helpful to the beginner who like me!

Note < > : More Wonderful tutorials please focus on Triple programming

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.