1

I am using Python (specifically the chess package) to make an AI chess game. I want to convert the console into the pieces positioning on html/css.

I've looked at the docs and there appears to be no built-in way. I have tried the alternative of styling the board directly in the console but it didn't work. https://python-chess.readthedocs.io/en/latest/ <-- Here are the docs Also it is important to know that I do not necessarily want to go the IPython/Jupyter route.

Okiso
  • 11
  • 4
  • What do you mean by "front end"? Do you have a BE and FE applications and they are communicating using REST api? – Gameplay Mar 28 '23 at 12:39
  • @okiso please elaborate on your question. It's ambiguous. – Muhammad Afzaal Mar 28 '23 at 12:41
  • Essentially I want to be able to read the console and convert it to the positioning of elements on a simple website, not I do not currently have that set up, but I was planning on designing that around this code. I am just planning to hard-code an output converter. – Okiso Mar 28 '23 at 12:47
  • This does not answer the first comment. How do you plan to get the rendering on the "website"? Are you saying you want to copy from the console output manually to your website? This step is missing in your problem description. – trincot Mar 28 '23 at 13:23
  • I would like to dynamically update the website based on each output from the console – Okiso Apr 01 '23 at 14:39

1 Answers1

1

The chess package has chess.svg which provides a HTML generating capability:

import chess
import chess.svg

board = chess.Board()
for move in "e4 e5 Qh5 Nc6 Bc4 Nf6 Qxf7".split(" "):
    board.push_san(move)
html = chess.svg.board(board)

The html will be a string with a <svg> tag, which can be placed in a HTML document.

You can see how that <svg> element renders in a page on this jsfiddle

trincot
  • 317,000
  • 35
  • 244
  • 286
  • Hm, I understand defining the html variable, but how do I render it on the HTML side of it, you seem to have hard-coded a single board, how would I make it dynamic? – Okiso Mar 30 '23 at 19:35
  • This is just an example. Of course I am not suggesting you should hard code a game. I was in the understanding you already have dynamic games that you can output to console. This is just demoing that instead of outputting the game as text, you can output it as HTML. – trincot Mar 30 '23 at 19:37
  • As to rendering the HTML, that was the subject I was asking about in comments below your question, but I got no answer. This is typically done by setting up HTTP request/response where the client performs an Ajax call and the server -- running Python -- sends the response HTML. This is what the first comment below your question was about. – trincot Mar 30 '23 at 19:39
  • I have mostly used js in the past so I am less familiar with this area of coding, is there any simpler way to do it? – Okiso Apr 01 '23 at 14:39
  • If you are familiar with JS, then realise that Ajax is a fundamental part of communicating with a server application. If this is out of your reach, then do everything on the client side. There are also chess libraries for JavaScript. But this is out of scope of this question, as you were asking about Python and the chess package. – trincot Apr 01 '23 at 14:57
  • well I use it either in html5 or completely on its own so I have never ventured into such, can you link the docs so I can see if I can understand them? – Okiso Apr 01 '23 at 15:42
  • I don't know what you refer to? Which docs? – trincot Apr 01 '23 at 16:00
  • Well docs is probably not the right word, I meant the information about ajax as I am new to it. – Okiso Apr 03 '23 at 13:30
  • Just search the site. For instance [How to implement a minimal server for AJAX in Python?](https://stackoverflow.com/q/336866/5459839), [How to set up Python server side with javascript client side](https://stackoverflow.com/q/11727145/5459839). People use frameworks like Flask or Django. – trincot Apr 03 '23 at 14:16