I’m automating some data processing and creating jira tickets out of it.
Pandas does have to_html
or to_csv
or even to_markdown
. But jira supports only wiki markup for creating a table.
e.g.
<!-- wiki markup -->
||header1||header2||header3||rn|cell 11|cell 12|cell 13|rn|cell 21|cell 22|cell 23|
will create
header1 | header2 | header3 |
---|---|---|
cell 11 | cell 12 | cell 13 |
cell 21 | cell 22 | cell 23 |
Is there anyway to convert pandas dataframe to wiki markup table to be used in Jira?
I’m keeping df.iterrows
as Last resort since iterating over dataframe is not a recommended solution as per answers in How can I iterate over rows in a Pandas DataFrame? Since my expected dataframe is small, iteration should be fine in my case. This question can be considered as more of a curiosity what can be done in case of larger dataframes.
2
Don’t reinvent the wheel, tabulate
supports a jira template:
from tabulate import tabulate
tabulate(df, headers='keys', tablefmt='jira', showindex=False)
Output:
'|| header1 || header2 || header3 ||n| cell 11 | cell 12 | cell 13 |n| cell 21 | cell 22 | cell 23 |'
If you really want the rn
line separator:
tabulate(df, headers='keys', tablefmt='jira', showindex=False).replace('n', 'rn')
You can write a function that does this:
import pandas as pd
data = {
"header1": ["cell 11", "cell 21"],
"header2": ["cell 12", "cell 22"],
"header3": ["cell 13", "cell 23"],
}
df = pd.DataFrame(data)
print(df)
def df_to_jira_wiki(df):
headers = "||" + "||".join(df.columns) + "||"
rows = "rn".join(["|" + "|".join(map(str, row)) + "|" for row in df.values])
return headers + "rn" + rows
jira_markup = df_to_jira_wiki(df)
jira_markup
Which returns
'||header1||header2||header3||rn|cell 11|cell 12|cell 13|rn|cell 21|cell 22|cell 23|'
or
||header1||header2||header3||
|cell 11|cell 12|cell 13|
|cell 21|cell 22|cell 23|
in a printed version