Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown output polluted with previous style setting #341

Open
bmihaila-bd opened this issue Oct 31, 2024 · 6 comments
Open

Markdown output polluted with previous style setting #341

bmihaila-bd opened this issue Oct 31, 2024 · 6 comments
Labels
bug Something isn't working

Comments

@bmihaila-bd
Copy link

What did you do?

Emit a table in markdown format

What did you expect to happen?

The markdown output should be parsable and rendering as a table in markdown viewers (e.g. Github)

What actually happened?

The markdown output is not rendered at all as a table

What versions are you using?

  • PrettyTable:
    3.11.0

Explanation

It seems that previous styles are not cleaned up when setting the MARKDOWN style, so they mess up the markdown output.

# code goes here
table.set_style(prettytable.SINGLE_BORDER)
...
table.set_style(prettytable.MARKDOWN)
# results in below output, with corners and some line connectors coming from the BORDER style. That breaks the markdown parsers.
┌ :───────────────────: ┐
| Namespace    |  Count |
├ :────────────|──────: ┤
| bla      |     24 |
| blu |      1 |
| blam       |      1 |

The simple fix seems to be to call table._set_default_style() in the def _set_markdown_style(self) method. That results in this markdown parsable output:

| Namespace    | Count |
| :------------|-----: |
| bla       |    24 |
| blu |     1 |
| blam       |     1 |

Below I can show how both above look with the Github parser here:
broken:
┌ :───────────────────: ┐
| Namespace | Count |
├ :────────────|──────: ┤
| bla | 24 |
| blu | 1 |
| blam | 1 |

fixed:

Namespace Count
bla 24
blu 1
blam 1
@bmihaila-bd bmihaila-bd changed the title Markdown output with previous style Markdown output polluted with previous style setting Oct 31, 2024
@hugovk
Copy link
Collaborator

hugovk commented Oct 31, 2024

Repro

Here's self-contained script to reproduce:

from prettytable import TableStyle, PrettyTable

table = PrettyTable()
table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
table.add_row(["Adelaide", 1295, 1158259, 600.5])
table.add_row(["Brisbane", 5905, 1857594, 1146.4])
table.add_row(["Darwin", 112, 120900, 1714.7])
table.add_row(["Hobart", 1357, 205556, 619.5])
table.add_row(["Sydney", 2058, 4336374, 1214.8])
table.add_row(["Melbourne", 1566, 3806092, 646.9])
table.add_row(["Perth", 5386, 1554769, 869.4])

table.set_style(TableStyle.SINGLE_BORDER)
table.set_style(TableStyle.MARKDOWN)

print(table)

Outputs:

| City name | Area | Population | Annual Rainfall |
├ :───────: | :──: | :────────: | :─────────────: ┤
|  Adelaide | 1295 |  1158259   |      600.5      |
|  Brisbane | 5905 |  1857594   |      1146.4     |
|   Darwin  | 112  |   120900   |      1714.7     |
|   Hobart  | 1357 |   205556   |      619.5      |
|   Sydney  | 2058 |  4336374   |      1214.8     |
| Melbourne | 1566 |  3806092   |      646.9      |
|   Perth   | 5386 |  1554769   |      869.4      |

But doesn't render properly:

| City name | Area | Population | Annual Rainfall |
├ :───────: | :──: | :────────: | :─────────────: ┤
| Adelaide | 1295 | 1158259 | 600.5 |
| Brisbane | 5905 | 1857594 | 1146.4 |
| Darwin | 112 | 120900 | 1714.7 |
| Hobart | 1357 | 205556 | 619.5 |
| Sydney | 2058 | 4336374 | 1214.8 |
| Melbourne | 1566 | 3806092 | 646.9 |
| Perth | 5386 | 1554769 | 869.4 |

Workaround

Set the default style in between:

from prettytable import TableStyle, PrettyTable

table = PrettyTable()
table.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
table.add_row(["Adelaide", 1295, 1158259, 600.5])
table.add_row(["Brisbane", 5905, 1857594, 1146.4])
table.add_row(["Darwin", 112, 120900, 1714.7])
table.add_row(["Hobart", 1357, 205556, 619.5])
table.add_row(["Sydney", 2058, 4336374, 1214.8])
table.add_row(["Melbourne", 1566, 3806092, 646.9])
table.add_row(["Perth", 5386, 1554769, 869.4])

table.set_style(TableStyle.SINGLE_BORDER)
table.set_style(TableStyle.DEFAULT)  # <!-- Add this
table.set_style(TableStyle.MARKDOWN)

print(table)

Outputs:

| City name | Area | Population | Annual Rainfall |
| :-------: | :--: | :--------: | :-------------: |
|  Adelaide | 1295 |  1158259   |      600.5      |
|  Brisbane | 5905 |  1857594   |      1146.4     |
|   Darwin  | 112  |   120900   |      1714.7     |
|   Hobart  | 1357 |   205556   |      619.5      |
|   Sydney  | 2058 |  4336374   |      1214.8     |
| Melbourne | 1566 |  3806092   |      646.9      |
|   Perth   | 5386 |  1554769   |      869.4      |

Renders:

City name Area Population Annual Rainfall
Adelaide 1295 1158259 600.5
Brisbane 5905 1857594 1146.4
Darwin 112 120900 1714.7
Hobart 1357 205556 619.5
Sydney 2058 4336374 1214.8
Melbourne 1566 3806092 646.9
Perth 5386 1554769 869.4

@hugovk hugovk added the bug Something isn't working label Oct 31, 2024
@hugovk
Copy link
Collaborator

hugovk commented Oct 31, 2024

We should probably call _set_default_style() at the start of set_style() for all styles (and remove _set_default_style() from the start of _set_orgmode_style().

Would you like to submit a PR with tests?

@bmihaila-bd
Copy link
Author

Yes that would make sense to make sure all other styles are clean, too.

@bmihaila-bd
Copy link
Author

Regarding doing the PR, sadly I don't have the time for that at the moment, sorry. I added the workaround in my code to call _set_default_style() and moved on to next things. Can try in a couple of weeks but hard to promise a timeline.

@hugovk
Copy link
Collaborator

hugovk commented Nov 1, 2024

Sure, no rush :)

As a workaround, you might prefer to call set_style(TableStyle.DEFAULT) rather than the internal _set_default_style().

@MonstersInc-sudo
Copy link

@hugovk , I took a look at the code and added some logic for resetting defaultsbefore going to if statement in set_style along with taking out redundant resets in the _set_orgmode method.

Pull request is here. PR-344. Will upload tests shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants