Reputation: 574
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?
Upvotes: 6
Views: 14691
Reputation: 29
This solution resorting to IPython is working on Jupyterlab 4.0.7:
from IPython.display import display, clear_output
clear_output(wait=True)
display(boardsvg)
Here is a complete example inspired from the doc of python-chess:
import chess.engine
from IPython.display import display, clear_output
board = chess.Board()
engine = chess.engine.SimpleEngine.popen_uci(r"/usr/local/Cellar/stockfish/16/bin/stockfish")
while not board.is_game_over():
result = engine.play(board, chess.engine.Limit(time=0.1))
#print(result.move)
board.push(result.move)
boardsvg = chess.svg.board(board, size=350)
clear_output(wait=True)
display(boardsvg)
engine.quit()
where /usr/local/Cellar/stockfish/16/bin/stockfish
is the path to the stockfish engine installed on my mac (with brew install stockfish
).
Note that you must have installed the chess
package previously on your jupyter, e.g. by running %pip install chess
in a code cell).
Upvotes: 0
Reputation: 56660
The python-chess library also contains the chess.svg
package that can output SVG images. Here's an example adapted from the documentation:
from cairosvg import svg2png
import chess.svg
board = chess.Board("8/8/8/8/4N3/8/8/8 w - - 0 1")
svg_text = chess.svg.board(
board,
fill=dict.fromkeys(board.attacks(chess.E4), "#cc0000cc"),
arrows=[chess.svg.Arrow(chess.E4, chess.F6, color="#0000cccc")],
squares=chess.SquareSet(chess.BB_DARK_SQUARES & chess.BB_FILE_B),
size=350)
with open('example-board.svg', 'w') as f:
f.write(svg_text)
svg2png(bytestring=svg_text, write_to='example-board.png')
It also converts the SVG image to PNG with cairosvg
. That produces this PNG image:
Upvotes: 1
Reputation: 21
In Google Colab you can use the display() function.
board = chess.Board()
display(board)
Upvotes: 1
Reputation: 41
you can use chess.svg
import chess.svg
board = chess.Board()
chess.svg.board(board, size=350)
Upvotes: 4
Reputation: 73
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.
Upvotes: 6