Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • Nov 28 00:05
  • Nov 27 17:00
    sxre closed #941
  • Nov 27 17:00
    sxre commented #941
  • Nov 27 09:19
    niklasf edited #931
  • Nov 27 09:17
    niklasf commented #931
  • Nov 27 09:15
    niklasf commented #931
  • Nov 27 09:09

    niklasf on master

    Mention that parse_san is not s… (compare)

  • Nov 27 07:51
    niklasf edited #941
  • Nov 27 07:49
    niklasf commented #941
  • Nov 26 22:00
    sxre opened #941
  • Nov 23 19:08
  • Nov 22 18:57
    niklasf closed #936
  • Nov 22 18:57

    niklasf on master

    Simplify SVG board fill example… (compare)

  • Nov 22 18:10
    niklasf commented #939
  • Nov 22 18:09
    niklasf closed #939
  • Nov 22 18:09
    niklasf commented #939
  • Nov 22 18:09
    niklasf labeled #939
  • Nov 22 18:09
    niklasf commented #940
  • Nov 22 18:08
    niklasf closed #940
  • Nov 22 18:08

    niklasf on master

    PettingZoo now on https://petti… (compare)

Niklas Fiekas
@niklasf
the output should be svg tiny 1.2 compatible, so deprecations from svg 2 don't apply, yet
Niklas Fiekas
@niklasf
i guess we can have both, according to https://www.w3.org/TR/SVG2/linking.html#XLinkRefAttrs
Ghost
@ghost~58c242a5d73408ce4f4f9df5
Very good! Thanks for the patch. This means python-chess now conforms to SVG Tiny 1.2 AND SVG 2.0 standards. That's a very welcome change.
Ghost
@ghost~58c242a5d73408ce4f4f9df5

@niklasf, please fix the alignment of white and black pawns, because their offset from the center of a square on the x axis is exactly 0.5 pixels to the left.

I have fixed this by editing the translate() data of an already generated SVG and added 0.5 pixels to the x attribute.

<use href="#white-pawn" transform="translate(15, 285)"/>
was fixed to

<use href="#white-pawn" transform="translate(15.5, 285)"/>

Ghost
@ghost~58c242a5d73408ce4f4f9df5
I don't know what exactly to fix in svg.py so that pawns (white and black) are correctly centered in the square. I don't know whether SVG path data needs to be fixed or the formula for the x and y (svg.py, lines 371 & 372).
Ghost
@ghost~58c242a5d73408ce4f4f9df5
image.png
Ghost
@ghost~58c242a5d73408ce4f4f9df5
As you can see from the screenshot image above, blue dots mark legal chess moves for the knight on f6. Notice the blue dot on the white pawn on e4. You can clearly see (well, if you look closely) that the white pawn is off center. The position of the circle is x=22.5 and y=22.5 which is the center of a square (which defaults to 45). I have monkey-patched the chess.svg.XX attribute to have this value: "<g id="xx"><circle r="5.0" cx="22.5" cy="22.5" fill="#0000ff"></circle></g>"
Ghost
@ghost~58c242a5d73408ce4f4f9df5
@niklasf, line 169 in svg.py is "version": "1.1". Shouldn't that be "version": "1.2", since the SVG documentation in the SVG rendering section says "The chess.svg module renders SVG Tiny 1.2 images"?
Niklas Fiekas
@niklasf
done via niklasf/python-chess@e7d07fd. i'll check out the pawn positions soonish
Niklas Fiekas
@niklasf
and pawns done via niklasf/python-chess@1d52ea6
Ghost
@ghost~58c242a5d73408ce4f4f9df5
Amazing work, @niklasf! Can you push a new version? Please?
Ghost
@ghost~58c242a5d73408ce4f4f9df5
The changes since v1.5.0 deserve a v1.5.1 version. Do you agree?
Also, GitHub changed the name of the 'master' branch to be named 'main' now. Can we accomodate that change?
Ghost
@ghost~58c242a5d73408ce4f4f9df5
@niklasf, thanks for v1.6.0!
Ghost
@ghost~58c242a5d73408ce4f4f9df5
@niklasf, are you willing to implement a way for us to inject JavaScript into the generated SVG? Just like there's a style argument in chess.svg.board(), it would be beneficial to have a script argument as well. If you unban me, I can make a PR.
Niklas Fiekas
@niklasf
no, but you can use an xml.etree or so to insert custom elements
Ghost
@ghost~58c242a5d73408ce4f4f9df5
I don't understand how should I even go and start manipulating your SVG document (chess.svg). I would like to add event listeners to your svg so that some drag & drop functionality can be implemented. Is that even possible? If yes, PLEASE GIVE AN EXAMPLE.
Niklas Fiekas
@niklasf
no, i told you multiple times it's not meant to do that, and this will be the last time
Ghost
@ghost~58c242a5d73408ce4f4f9df5
I have a question. Is it by design (and normal?) that a chess engine also returns its played move in the principal variation info list (i.e., in the result.info["pv"] list of chess.Move objects)? So, from the initial position of Standard Chess, if White plays 1.e4 and an engine responds with 1...e6, the result.info["pv"] list of chess.Move objects is then [chess.Move.from_uci("e7e6"), ..., ...]. Is it possible that it only outputs principal variation moves AFTER its own move in the principal variation info list (so, basically, from the pondermove onwards [hence, truncating its own played move])?
1 reply
Ghost
@ghost~58c242a5d73408ce4f4f9df5

@niklasf, there's a bug in chess.svg, namely on line 71 (CHECK_GRADIENT). The "r" attribute is missing there, so PyQt6 doesn't render the radial gradient at all and produces an error. PyQt5, for some reason, works without the "r" attribute. And as tested, adding the "r" attribute works in both frameworks. Adding the "r" attribute doesn't brake anyone's code, so it's safe to add it.

Please fix line 71 in chess.svg to be:

CHECK_GRADIENT = """<radialGradient id="check_gradient" r="0.5"><stop offset="0%" stop-color="#ff0000" stop-opacity="1.0" /><stop offset="50%" stop-color="#e70000" stop-opacity="1.0" /><stop offset="100%" stop-color="#9e0000" stop-opacity="0.0" /></radialGradient>""" # noqa: E501

2 replies
Ghost
@ghost~58c242a5d73408ce4f4f9df5

How to get the best-understandable score of a chessboard position (i.e., a centipawn score in the format like 0.41) returned by result.info["score"].white().score() (which returns a not-so-understandable value in the format like 41.00)?

Well, my approach is that I just divide the returned value of that score() method by 100. So like this: result.info["score"].white().score() / 100 (I prefer to have centipawn scores from White's point of view).

There's no mention of this "formula" (dividing by 100) in the python-chess documentation. There could (maybe even should) be a method (named perhaps centipawn_score()) that divides the returned value of that score() method by 100. It'd be used as result.info["score"].white().centipawn_score() to get the actual (centipawn) score (like 0.41, as it is displayed by all chess GUIs I know) and not a (perhaps strange) number (like 41.00).

Can this be implemented?

Ghost
@ghost~58c242a5d73408ce4f4f9df5
I would fix the method score() of the Cp class in chess.engine on line 644 and define it like this:
def score(self, *, mate_score: Optional[int] = None) -> float:
    return self.cp / 100
Ghost
@ghost~58c242a5d73408ce4f4f9df5
@niklasf, I think it would be more reasonable for the find_move() method of chess.Board() to return None if no legal move is found by it. It now raises a ValueError exception, but I think that could be improved. It's better to do if board.find_move(some_move) is not None than to have a try/except block.
Sam Q
@SamQ26867433_twitter
Hi, I'd like to know how to change the colors of the light and dark chessboard squares. An example would be nice.
Sam Q
@SamQ26867433_twitter
I know I can go to the svg.py file and change the light and dark colors there, but I'd like to know how do do it without modifying the python-chess library.
Ghost
@ghost~58c242a5d73408ce4f4f9df5
@SamQ26867433_twitter, the way I do it is like this:
  1. firstly, I define a Python dictionary and override those default python-chess hex values that represent colors
    my_default_theme = {
     "coord": "#f0f6fd80",
     "margin": "#15781b80",
     "arrow red": "#88202080",
     "arrow blue": "#00308880",
     "arrow green": "#15781b80",
     "square dark": "#e5e5e5ff",
     "square light": "#f0f6fdff",
     "arrow yellow": "#e68f0080",
     "square dark lastmove": "#8b000080",
     "square light lastmove": "#8b000080",
    }
  2. then I just pass this my_default_theme dictionary to chess.svg.board(..., colors=my_default_theme)
Sam Q
@SamQ26867433_twitter
@PedanticHacker Is there any way to have a dynamically resizable chessboard in python-chess? I tried to do it with QSplitters, but it didn't work. Also, I read svg was causing problems when using pyinstaller. If true, it's a deal-breaker using svg, the python-chess is still useful anyway.
Ghost
@ghost~58c242a5d73408ce4f4f9df5

@SamQ26867433_twitter, you need to have the chessboard quadraticly shaped, otherwise you end up with squares that are not squares anymore but rectangles, and this messes up the coordinate system of your chessboard; clicking on the a1 square, for example, is registered as being the a2 square, etc.

I propose you have options for displaying a small chessboard, a medium chessboard and a big chessboard, each of its size being quadraticly shaped; say, 400×400 chessboard size for the small version, 500×500 for the medium version and 600×600 for the big version... you then have complete control over the coordinate system of your chessboard, because your coordinate formula takes the size of the chessboard (whichever of those 3 sizes is chosen by the user of your application), and then every square is registered correctly, because the formula adapts to whichever size you pass into it, but it must be quadratic (400×400, 500×500, etc.)

you can't freely stretch and shrink the chessboard, that's a limitation of the chessboard (and its squares of which it is comprised) being naturally quadratic...

Ghost
@ghost~58c242a5d73408ce4f4f9df5
@niklasf, what is happening with the development of python-chess? there hasn't been a new version for more than 2 months now... I know you probably were on vacation, but is the development of python-chess going to continue? perhaps in September?
Niklas Fiekas
@niklasf
no worries. i don't have any major plans for new features at the moment, but definitely still actively maintaining the project
Ghost
@ghost~58c242a5d73408ce4f4f9df5
@niklasf, can you please release a new version? Perhaps a v1.6.2? I have to monkey-patch the chess.svg.CHECK_GRADIENT for it to have r="0.5". As I already explained, (I don't know why, but) PyQt6 needs this "r" attribute explicitly set, even though a default is already set as r="0.5" as per the SVG specification). This was already fixed by you, but if you release a new version, my monkey-patch will no longer be needed.
Niklas Fiekas
@niklasf
you can just install from git in meantime (pip install git+https://github.com/niklasf/python-chess)
Ghost
@ghost~58c242a5d73408ce4f4f9df5

I've done that. Thanks for the tip.

@niklasf, what will the next version number of python-chess be? Are you waiting for the final version of Python 3.10 to then drop Python 3.9 support and bump python-chess to version v2.0.0?

Ghost
@ghost~58c242a5d73408ce4f4f9df5
@niklasf, you made a small error in your last python-chess commit. There should be no hyphen in the sentence "Probes WDL tables for win/draw/loss-information under the 50-move rule,". This sentence should read as "Probes WDL tables for win/draw/loss information under the 50-move rule,"). I know I'm a pain in the butt, but I like precision.
Also, a comma is missing in this sentence: "However the lines are not always the most straightforward ways to win.". I'm sure you agree that there should be a comma after the word "However", so the sentence should read as "However, the lines are not always the most straightforward ways to win."
1 reply
Niklas Fiekas
@niklasf
2.x is a long way off, and i won't drop support for even python 3.7, unless there's a good reason to
Ghost
@ghost~58c242a5d73408ce4f4f9df5

I now additionally found 2 missing commas in this sentence in the Syzygy docs (in the chess.syzygy.Tablebase.add_directory docstring):
"By default all available tables with the correct file names (e.g. WDL files like KQvKN.rtbw and DTZ files like KRBvK.rtbz) are added."

I'm sure you can agree that this sentence is orthographically more correct:
"By default, all available tables with the correct file names (e.g., WDL files like KQvKN.rtbw and DTZ files like KRBvK.rtbz) are added."

Ghost
@ghost~58c242a5d73408ce4f4f9df5

In the header of this Gitter conversation, there is this text: Discussing https://github.com/niklasf/python-chess, a pure Python chess library

I would remove the word purehere, since it was removed in the python-chess project description on GitHub.

Ghost
@ghost~58c242a5d73408ce4f4f9df5

@niklasf, thank you for bumping the python-chess version to v1.7.0! :)

I must admit that I am a little bit disappointed that I wasn't appreciated for the CHECK_GRADIENT fix (for pointing out the bug and providing the fix) in chess.svg, so that now CHECK_GRADIENT is compatible with QtSvg (because the radialGradient element has an explicit r=0.5 attribute).

Also, I care a lot about python-chess (as you can probably notice), so I don't understand why my ban on GitHub hasn't been lifted yet (if it ever will be).

Can I at least get an explanation why I got banned in the first place? All I ever wanted was to make python-chess better. If I was ever rude or intrusive, I am sorry.

Niklas Fiekas
@niklasf
see https://github.com/niklasf/python-chess/issues/725#issuecomment-754783191. not going to discuss it here. and sorry, i have occasionally explicitly thanked contributors in the changelog, but that's exceptional and not its primary function
Ghost
@ghost~58c242a5d73408ce4f4f9df5
And you're not going to unban me?
Ghost
@ghost~58c242a5d73408ce4f4f9df5
Anyway, the cyclomatic complexity of the code in chess.svg is VERY BAD, it is of grade F (the worst) as reported by radon (having 43 branches, produced by all the if-elif-else statements in there + other things)... If you don't wanna unban me, then YOU reduce the cyclomatic complexity in chess.svg, if you care about those things at all. If not, then I guess you don't cherish good code.
Sam Q
@SamQ26867433_twitter
@niklasf , I'm developing a chess app & have a nice board where I can manually make moves on it with the mouse(integrated with Python-Chess lib). When a move is made I want to save it to a list. The list will be used to walk backward & forward through all moves made using arrow keys. This list will also be saved to a database. I'm at a crossroads now, where I need to know the best way to use Python-Chess to support the above scenario. I've considered having all moves saved to the list as FENs(?!) ; but that may not be the best way; it's not clear to me; please advise.
Ghost
@ghost~58c242a5d73408ce4f4f9df5
@SamQ26867433_twitter, python-chess already saves moves in a list. The list is board.move_stack if board is a chess.Board() instance. Also, to get a FEN string, use board.fen() function.
Sam Q
@SamQ26867433_twitter
@PedanticHacker , Thank you so very much! Especially for the swift response! One more question: Is there a way to turn a FEN into a move for board.push() ?
Ghost
@ghost~58c242a5d73408ce4f4f9df5
Is there a way to turn a FEN into a move for board.push() ?
@SamQ26867433_twitter, unfortunately, no. A FEN string is just a representation of a chess board's position, i.e., how chess pieces are placed on the chess board + some other data, but python-chess can't determine a chess move that was last played on the chess board just by giving python-chess a FEN string. That's not possible, even in other chess libraries. You need a PGN. In PGN, you have every chess move recorded, so you can shuffle between moves back and forth. And python-chess has full support for PGN handling and writing, it has a bunch of functions for every scenario that you would want or need. Everything is explained here: https://python-chess.readthedocs.io/en/latest/pgn.html#pgn-parsing-and-writing
Sam Q
@SamQ26867433_twitter
@PedanticHacker Thank you again for such a lucid explanation!
Sam Q
@SamQ26867433_twitter
@PedanticHacker Using chess.Board().move_stack, I can save lists of moves for each opening line in a database(DB). The moves in the list are saved as Move.from_uci('xxxx'). I'd like to display the opening lines in a tree showing the branching offshoots of each line in the overall set of opening lines; not sure what functions in Python-Chess lib to use for accomplishing this. Do I need to convert the saved lines in the DB into pgn form first before attempting to create the tree? What functions already do this? Is there a better way than what I'm considering?
Ghost
@ghost~58c242a5d73408ce4f4f9df5
@SamQ26867433_twitter, I don't understand exactly what is your question. Do you want to show the name of a chess opening for a particular chess move? Is that what you are asking?