My first full stack data science project (Part 3)…

Bhavya Rajdev
4 min readFeb 20, 2022

--

Link to previous article: Here
Link to next article: Here

In the previous article we wrote functions to convert our CSV file to MySQL database and functions to add new users into database and add new data of existing user. In this article we'll write functions that be used to handle user clicks on a website like move clicks, user data search, rating data search and more. Basically it's going to be a file that'll store the data for current position as global variables, functions like move_clicks(string) and go_back() will be there to change the current position accordingly and get_on_display() will return the data that'll be displayed to user. This is the file that's going to interact with database, handle all the data related stuff and answer what to be displayed. This is ‘backend.py’ file in the source code. I've set it up such that you can debug easily.

First of all what kind of data we want to display to user? User will see on the screen, on current position for all the moves that are being played in at least one game, the move notation, total number of games played in this move order, the win percentage of white, win percentage of black and draw percentages.

So what should be the format of data returned by get_on_display function? It's a dictionary of key=move_name, value=(another dictionary: key=-1(draw), 0(black won) and 1(white won), and value=number of games played)

What will the user be allowed to do in a website? User will be able to get data for a specific username, data of particular rating range, click on a move, go back a move, change color and change the time class from rapid to blitz to bullet.

First thing is to initialize the user data (dataframe of moves vs white_won). We'll write function to convert time control column to time class which has some rules given and filter the dataframe according to user requirement. User data as white and black are both different so we also want to separate dataframes by user_white column.

Example of df_white:                                            moves  white_won  
e4, e5, d4, exd4, Bc4, Bc5, Bxf7+, Kxf7, Qh5+,... 1
e4, e6, d4, d5, e5, c5, c3, Nc6, Nf3, Qb6, Be2... 0
e4, e5, Nf3, d6, Bc4, Be6, Bxe6, fxe6, d4, exd... 0
e4, e5, Nf3, Nc6, Bc4, Bc5, c3, Nf6, d4, exd4,... 1

Next thing is to initialize rating data which is a dataframe containing all the games played in given rating range. Same thing here as well, we want to filter the dataframe by time class provided by user.

Format of df_rating is same as above.
So global variables as of now storing the current state are df_white, df_black and df_rating.

We'll write a function to split the column 'moves' into two parts. One part is left to the first comma as 'movesl' and other part is right to the first comma. Next thing is to group the dataframe by 'movesl' and 'white_won' which would already give the answer but let's convert it to JSON format and then return it. We are not doing all these with main dataframes but copy of that because original ones have another work to do which is to store the current state.

Example of data returned by above function:
{'e4': {-1: 10, 0: 15, 1: 20}, 'd4': {0: 4, 1: 25}}

get_on_display_user and get_on_display_rating are the two functions that will be used to get the data of what should be displayed to user without doing changes in the current data. ‘currcol’(True-white, False-black) variable stores which color’s data user wants. This can be changed by user request.

‘stack’ will store all three dataframes of current state. On move clicks we push the dataframes into stack and on go back clicks we pop from stack and set all three dataframes as popped ones.
move_click function will take input as move string. We push the dataframes first of all into stack so that we are able to come back to same position again. Now we can safely make changes in the current main dataframes.
Example: say there are two games in dataframe: 'e4, e5, Nf3,...' and 'd4, d5, Nc3,...'. Say move 'e4' was clicked We drop the second game as 'e4' was not played in that game. Next set of moves in the first game are: 'e5, Nf3,...' Algorithm: We split the dataframe first and filter it on 'movesl' by given move. We also want to drop games which has no moves played after this one. We do this for all three of them.

change_color function as the name says will change change the currcol from true to false and false to true.

go_back function will only be called if it's not starting position. We pop once from stack and set current dataframes as popped ones.

What if the username is not there in our database. We just add all those list of users who searched on website and didn't get their data. We keep track of those users so that we can make their data available soon.

We’re done with building the website model. In the next article we’ll build website frontend which will include designing website, encoding chess rules, decoding move strings and more. Link to next article: Here

--

--