5

I am working a chess game based on this library: https://pypi.org/project/python-chess/ or https://github.com/niklasf/python-chess

On Jupyter Notebook, if I run this code:

import chess
board = chess.Board()
board

It will display a nice board (i.e. with colors, shape, looking like a chess board). If I run like this:

import chess
board = chess.Board()
print(board)

It will display the board in a much more rudimental way with letters.

The problem is that the only way of seeing the nice board, using the "board" command, is if I am using Jupyter Notebook. If I try to run on Visual Studio or line command the command "board" nothing will happen. It seems that the line command will not support the use of "board" (from their website: Supports Python 3.6+ and PyPy3.IPython/Jupyter Notebook integration).

Is there a way around this? In other words, can I still run "board" on command line and visualize the nice chess board?

FFLS
  • 565
  • 1
  • 4
  • 19
  • You can't do in command line, just jupyter notebook. Please check https://github.com/niklasf/python-chess/blob/master/chess/svg.py. python-chess create svg file... – stars seven Mar 03 '20 at 05:25

3 Answers3

6

If all you want to do is simply graphically display a position, you can install and use the chess-board package. I use it in tandem with the one you are talking about in the following example:

import chess

from chessboard import display
from time import sleep

board = chess.Board()

move_list = [
    'e4', 'e5',
    'Qh5', 'Nc6',
    'Bc4', 'Nf6',
    'Qxf7'
]

display.start(board.fen())
while not display.checkForQuit():
    if move_list:
        board.push_san(move_list.pop(0))
        display.update(board.fen())
    sleep(1)
display.terminate()

This package however currently feels quite lacking when it comes to flexibility and well... sanity. You would have to at least open the packages display.py file and add one line at the beginning of the start() function. Otherwise, you will not be able to use display.update().

def start(fen=''):
    global gameboard

I see you posted this question a long time ago. Nonetheless, I hope you find this useful.

3

you can use chess.svg

import chess.svg
board = chess.Board()
chess.svg.board(board, size=350)
1

In Google Colab you can use the display() function.

board = chess.Board()
display(board)
davi133
  • 21
  • 3