0

I want to replay chess games in Portable Game Notation, which describes moves such as the King's Knight opening with

1. e4 e5 2. Nf3

I adapted FlaskChess in Python for this purpose and it can take a list of moves such as the equivalent of the King's Knight opening with:

e2e4
e7e5
g1f3

or it can take the current state of play in Forsyth-Edwards Notation, which describes the state of the board after the King's Knight opening with:

rnbqkbnr/pppp1ppp/8/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 0 2

I am looking for a Python library that converts the moves in Portable Game Notation to either of these to be replayed on a GUI. Does it already exist?

2

It seems like python-chess could be what you're looking for, unless I'm missing something. Specifically, check out the docs related to PGN.

Available via pip.

Here is an example for the King's Knight opening:

import io

import chess.pgn

pgn = io.StringIO("1. e4 e5 2. Nf3 *")

game = chess.pgn.read_game(pgn)

board = game.board()
for move in game.mainline_moves():
    print(move)
    board.push(move)  # to update and render the board

The result is:

e2e4
e7e5
g1f3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.