Python Tornado framework for data visualization tutorial

Source: Internet
Author: User

Python Tornado framework for data visualization tutorial

This article describes how to use the Python Tornado framework to visualize data. Tornado is an asynchronous and popular development framework. For more information, see

Extended modules used

Xlrd:

An extension tool for reading Excel in Python. You can read a specified form or cell.

Installation is required before use.

: Https://pypi.python.org/pypi/xlrd

Decompress the package and cd it to the decompressed directory. Execute python setup. py install.

Datetime:

Python built-in module for date and time operations

Functional modules to be implemented

Read the xls file and input it to the database

Obtain the on-duty status of the current day based on the year, month, and day parameters.

Pie Chart (number of persons on duty on the current day/Number of persons on duty not completed on the current day)

Waterfall chart (the duty status of all personnel on duty on the day)

Obtain the duty status for the current month based on the year and month parameters.

Obtain the duty status of the current year based on the annual Parameter

Duty System

There are 6 classes every day:

Am-Am

Am-Am

PM-PM

PM-PM

PM-PM

PM-PM

Each person has at most one class per day.

Only the duty time and the first half of the hour are effective.

Punching is required at work and off work. If you do not have a punching card, it is deemed that you are not on duty.

Analyze an Excel table

My fingerprint attendance machine can export at most one month's logging records at a time. One problem is that this month may span two months or one year. For example, from January 1, to January 1, to January 1. Therefore, you must pay attention to this pitfall when writing a processing method.

Export table:

=. = It seems that no one is on duty, right.

Everyone is very lazy. T

Sign...

For a brief analysis,

The attendance record table is the third sheet of the file.

Row 3 has a start and end time

The fourth row is the number of all dates.

The following two rows: First behavior user information; second behavior attendance record

Ideas

It is decided to store related information using three collections:

User: user information, including id, name, and dept

Record: attendance record, including id (User id), y (year), m (month), d (day), check (punch record)

Duty: duty schedule, including id (number of weeks, for example, 1 indicates Monday), list (list of persons on duty id), user_id: ["start_time ", "end_time"] (user's duty start time and End Time)

Read the xls file and store new attendance records and new users to the database.

Query the corresponding record based on the year, month, and day parameters. query the duty schedule for the current day and obtain the attendance records of the employees on duty on the current day. Compare the punching time of the on-duty personnel with the duty time to determine whether the punching is normal, and calculate the actual duty time and actual duty percentage.

Then, the json format data is output and echarts is used to generate charts.

The Analysis of attendance records for the current month and current year is the same, but it may be a little complicated.

All the explanations and specific ideas are included in the source code comments. Please continue to read the source code ~

Source code

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

43

44

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

84

85

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 tornado. httpserver

Import tornado. ioloop

Import tornado. options

Import tornado. web

From tornado. options import define, options

 

Import pymongo

Import time

Import datetime

Import xlrd

 

Define ("port", default = 8007, help = "run on the 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)

 

 

Class MainHandler (tornado. web. RequestHandler ):

Def get (self ):

Pass

 

Class ReadHandler (tornado. web. RequestHandler ):

Def get (self ):

# Getting collection

Coll_record = self. application. db. record

Coll_user = self. application. db. user

# Reading an excel table

Table = xlrd. open_workbook ('/Users/ant/Webdev/python/excel/data.xls ')

# Read the logging sheet

Sheet = table. sheet_by_index (2)

# Read the range of punched in months

Row3 = sheet. row_values (2)

M1 = int (row3 [2] [5: 7])

M2 = int (row3 [2] [18: 20])

# Set the current year

Y = int (row3 [2] [0: 4])

# Set the current month to the first month

M = m1

# Read the date range of the punch card

Row4 = sheet. row_values (3)

# Initialize the last day

Lastday = row4 [0]

# Traverse the date in the fourth row

For d in row4:

# If the date is earlier than the previous date

# If the month is increased, the current month is changed to the second month.

If d <lastday:

M = m2

# If the current two months are December and January

# Indicates that the year has passed, so year + 1

If m1 = 12 and m2 = 1:

Y = y + 1

# Count with n, ranging from 3 to (total number of rows/2 + 1)

# (Total number of rows/2 + 1)-3 = total number of users

# Traverse all users

For n in range (3, sheet. nrows/2 + 1 ):

# Obtain the first line of the user, that is, the user information line.

Row_1 = sheet. row_values (n * 2-2)

# Getting user IDs

U_id = row_1 [2]

# Getting user names

U_name = row_1 [10]

# Getting user departments

U_dept = row_1 [20]

# Querying this user

User = coll_user.find_one ({"id": u_id })

# Create a new user if the user does not exist in the Database

If not user:

User = dict ()

User ['id'] = u_id

User ['name'] = u_name

User ['dept'] = u_dept

Coll_user.insert (user)

# Obtain the second line of the user, that is, the attendance record line.

Row_2 = sheet. row_values (n * 2-1)

# Obtain the subscript for changing the current date

Idx = row4.index (d)

# Obtain the attendance record of the current user date

Check_data = row_2 [idx]

# Initialize the empty attendance record list

Check = list ()

#5 characters in a group, traverse attendance records and store them to the attendance record list

For I in range (0, len (check_data)/5 ):

Check. append (check_data [I * 5: I * 5 + 5])

# Query the current user's daily records

Record = coll_record.find_one ({"y": y, "m": m, "d": d, "id": user ['id']})

# Update a record if the record exists

If record:

For item in check:

# Add a new attendance record to the previous record

If item not in record ['check']:

Record ['check']. append (item)

Coll_record.save (record)

# Insert a new record if the record does not exist

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

43

44

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

84

85

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 ):

# Get the parameters of year, month, and day

Y = self. get_argument ("y", None)

M = self. get_argument ("m", None)

D = self. get_argument ("d", None)

# Determine whether the parameters are complete

If y and m and d:

# Convert a parameter to an integer for ease of use

Y = int (y)

M = int (m)

D = int (d)

# Retrieve all records of the current day

Coll_record = self. application. db. record

Record = coll_record.find ({"y": y, "m": m, "d": d })

# Obtain the day of the week

Weekday = datetime. datetime (y, m, d). strftime ("% w ")

# Obtain the daily duty schedule

Coll_duty = self. application. db. duty

Duty = coll_duty.find_one ({"id": int (weekday )})

# Initialize empty Target records (on-duty personnel records on the current day)

Target = list ()

# Traverse all records of the current day

For item in record:

# When the user of the record has a duty task on the same day, calculate and store the task to the target array

If int (item ['id']) in duty ['LIST']:

# Obtain the user's duty start and end times by using the user ID

Start = duty [item ['id'] [0]

End = duty [item ['id'] [1]

# Calculate the duty time/second

Date1 = datetime. 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

# Initialize the actual duty percentage

Item ['per '] = 0

# Initialize the commuting time

Item ['start'] = 0

Item ['end'] = 0

# Traverse the user's logging

For t in item ['check']:

# When it is earlier than the duty time

If t <start:

# Calculating Time Difference

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

# When the punching time is within half an hour before the duty time

If dif <= 1800:

# Punch in at work

Item ['start'] = start

Elif t <end:

# If you are not at work

If not item ['start']:

# The current record time is the start and end times.

Item ['start'] = t

Else:

# Otherwise, the current record time is the off-duty punch-in time

Item ['end'] = t

Else:

# If you have timed out at work

If item ['start']:

# Calculating Time Difference

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

# When the punching time is within half an hour of the duty time

If dif <= 1800:

# Punch in after work

Item ['end'] = end

# Punching at work and off duty

If item ['start'] and item ['end']:

# Calculate the actual duty time

Date1 = datetime. datetime (y, m, d, int (item ['start'] [: 2]), int (item ['start'] [-2:])

Date2 = datetime. datetime (y, m, d, int (item ['end'] [: 2]), int (item ['end'] [-2:])

Dif = (date2-date1). seconds

# Calculated percentage (actual duty time/duty time)

Item ['per '] = int (dif/float (item ['length']) * 100)

Else:

# Abnormal commuting is deemed as not on duty

Item ['start'] = 0

Item ['end'] = 0

# Add records to the target array

Target. append (item)

# Output data

Self. render ("index.html ",

Target = target

)

 

 

Def main ():

Tornado. options. parse_command_line ()

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 ['per']}

}

{% End %}

}

Last

Currently, only the files are read and the on-duty status of the day is queried. Later, the small application will be written according to the previous plan.

Because of the privacy of a bunch of friends, the test file was not sent. However, if you want to run it, you can tell me that I will send the file to you.

A possible database insert statement: db. duty. insert ({"id": 5, "list": [], 1: ["", ""], 2 ", "22:00"]})

Hope it will be helpful for beginners like me!

Note <>: For more exciting tutorials, please pay attention to the help house programming

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.