-
-
Notifications
You must be signed in to change notification settings - Fork 166
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
Comments
ReproHere'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 | WorkaroundSet 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:
|
We should probably call Would you like to submit a PR with tests? |
Yes that would make sense to make sure all other styles are clean, too. |
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 |
Sure, no rush :) As a workaround, you might prefer to call |
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?
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.
The simple fix seems to be to call
table._set_default_style()
in thedef _set_markdown_style(self)
method. That results in this markdown parsable output:Below I can show how both above look with the Github parser here:
broken:
┌ :───────────────────: ┐
| Namespace | Count |
├ :────────────|──────: ┤
| bla | 24 |
| blu | 1 |
| blam | 1 |
fixed:
The text was updated successfully, but these errors were encountered: