Practical 丨 Use Mini Program·Cloud Development to Build a Mini Program for Querying College Entrance Examination Scores

Practical 丨 Use Mini Program·Cloud Development to Build a Mini Program for Querying College Entrance Examination Scores

Lead

In 2020, the number of applicants for the college entrance examination reached 10.71 million, a record high. As the college entrance examination this year comes to an end, we will introduce a small program based on cloud development to query the admission scores of the college entrance examination, hoping to provide convenience for candidates to fill in their applications.

Data Sources

A total of nearly 30w pieces of data are collected in the background of the applet, including the admission scores of various batches of liberal arts and sciences in most key universities in 2008-2019, and the adoption of the new curriculum standard volume one, the new curriculum standard volume two, and the new curriculum standard three volumes in 2008-2019 And most of the admission scores for some autonomous proposition provinces, from advance approval to higher vocational college approval, are more informative.

All data is collected from various universities and colleges and universities. Due to the huge amount of data, in order to improve the speed, the ThreadPoolExecutor in the concurrent.futures (requires Python3.5+) module is used to build a thread pool to perform multiple tasks concurrently.

The database uses PgSQL, and all data is stored in the newly built Gaokao database, under which there are two tables, university (admission points of colleges) and province (batch line of provinces).

Field

Explanation

name

School name

stu_loc

Birthplace

stu_wl

Arts and Sciences

pc

Admission batch

year

years

score

Average admission score

university table description

Field

Explanation

year

years

stu_loc

Candidate's location

stu_wl

Arts and Sciences

pc

batch

control

The lowest control line of this batch

province table description

30w data volume, multiple sites, concurrent crawling, data conflicts are inevitable, before inserting, first filter out incomplete data, for example, when inserting a university table, a piece of data lacks the pc field, then this record Should be abandoned.

The most serious problem is data duplication. The solution I adopted is to first query whether the data to be inserted already exists. The main code of the university table is (name, stu, stu_wl, pc, year), because the reality restricts a college to only There can only be one average admission score in one category and one batch in a year. If it does not exist, the final insertion is performed and the transaction is committed.

Background building

After getting 30w pieces of data, I plan to implement the Flask+PgSQL model in the backend, and it was even deployed on a company’s cloud server at that time, but after the small program was passed through the developer tool joint debugging, the small program was launched Encountered a big trouble: because the small program requires online operation and cannot access the background through the ip address, it must be accessed through the registered domain name. It is not troublesome to buy a domain name, but the domain name registration takes more than a week, and it takes more than a week. The college entrance examination is less than 5 days. When I was at a loss, I accidentally saw that the applet·cloud development could solve this problem perfectly, so I tried decisively!

Cloud development provides developers with complete native cloud support and WeChat service support, weakens the concept of back-end and operation and maintenance, does not need to build a server, and uses the API provided by the platform for core business development, which can achieve rapid launch and iteration. At the same time, this ability , Is compatible with the cloud services already used by the developer, and is not mutually exclusive.

In other words, as long as the data is imported into the backend of the applet, the data can be accessed through the API of the applet platform. I have learned about some third-party clouds before, but I did not expect that the applet has integrated the features of these third-party clouds. Function, this wave of points like it.

The next background work is mainly to import data. Querying the background of the applet shows that the background supports importing data in json or csv format. So I wrote a script to export the data from the local database to the json file:

import psycopg2
import json

# Connect to the pgsql database, to ensure privacy, the password has been hidden
conn = psycopg2.connect(database="gaokao", user="postgres", password="*******", host="127.0.0.1", port="5432")
cur = conn.cursor()

cur.execute('select stu_loc,year,stu_wl,pc,control from province')
result = []
query_res = cur.fetchall()
for i in query_res:
  item = {}
  item['stu_loc'] = i[0]
  item['year'] = i[1]
  item['wl'] = i[2]
  item['pc'] = i[3]
  item['score'] = i[4]
  result.append(item)
# indent=2 Control the indentation of json format
# ensure_ascii Control the normal display of Chinese
with open("province.json",'w', encoding="utf-8") as f:
  f.write(json.dumps(result, indent=2, ensure_ascii=False))

There is another point to explain here. The json format required by the applet backend is somewhat different from our usual json format. 1. all the contents of json cannot be included by [and], and each is included by {} There can be no commas between data items.

Use notepad++ to open the original json file and use the replace function to solve it. Replace'[' and']' with spaces, and replace '},' with'}'.

After the modification, by importing the json file in the backend of the applet, the backend setup is basically completed.

Small terminal writing

Regarding the writing of the applet, here is an introduction to the experience of page writing , that is, how to implement the interface in the figure below.

At first, I wanted to achieve this effect, but I had no idea at all. Finally, I got the idea from the custom modal pop-up window :

At the beginning, the layout corresponding to the drop-down box of "regional colleges" is hidden. It is controlled by hidden=true in the wxml file. Once you click the drop-down box of "regions/colleges", set hidden to false. If there are other If the hidden attribute of the layout corresponding to the drop-down box is false, set the hidden attribute of these layouts to true to hide other layouts.

Of course, the true or false here needs to be dynamically modified in js through setData() to render the modified data from the data layer to the view layer.

Simultaneously imitate the following example code to complete the business logic:

//The query may be slow, it is best to add a loading animation
wx.showLoading({
      title:'Loading',
    })
    const countResult = await db.collection('province').where({
      stu_loc: name,
      pc: pici,

    }).count()
    const total = countResult.total
   //The calculation needs to be divided into several times
    const batchTimes = Math.ceil(total/MAX_LIMIT)
   //An array of promises that carry all read operations
   //The first loop to get the promise array of the number of times of the cloud database
    for (let i = 0; i <batchTimes; i++) {
      const promise = await db.collection('province').where({
      stu_loc: name,
      pc: pici,
      }).skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
     //The secondary loop obtains all data and pushes it to the newResult array according to the data length of the obtained promise array
      for (let j = 0; j <promise.data.length; j++) {
      var item = {};
      item.code = i * MAX_LIMIT + j;
      item.name = promise.data[j].stu_loc;
      item.year = promise.data[j].year;
      item.wl = promise.data[j].wl;
      item.pc = promise.data[j].pc;
      item.score = promise.data[j].score;
      console.table(promise.data)
      newResult.push(item)
      }
    }
    if (newResult.length != 0) {
      that.setData({
      hasdataFlag: true,
      resultData: newResult
      })
    } else {
      that.setData({
      hasdataFlag: false,
      resultData: newResult
      })
    }
   //Hide loading animation
    wx.hideLoading()

Get the source code

Click on the lower left corner of the end of the article to read the original text to get the source code of this article and more cloud development practical cases.

Reference: https://cloud.tencent.com/developer/article/1660156 Actual Combat丨 Use Mini Program·Cloud Development to Build a Mini Program for Querying College Entrance Examination Scores-Cloud + Community-Tencent Cloud