mpastell / Pweave

Pweave is a scientific report generator and a literate programming tool for Python. It can capture the results and plots from data analysis and works well with numpy, scipy and matplotlib.
http://mpastell.com/pweave
Other
435 stars 64 forks source link

How to conditionally control markdown text in HTML report? #116

Closed CurtLH closed 6 years ago

CurtLH commented 6 years ago

I'm am using pweave to create a scientific report. Within the report, I am calculating a value and I want to control the markdown text that is shown based on the value. When using R-notebooks with knitr, I am able to do this by using asis in the code chunk. However, I cannot figure out how to accomplish this using pweave.

For example, if x <= 0.5, then show interpretation #1, but if x > 0.5, show interpretation #2.

In an R-notebook, I would do this as follows:

(I'm using single-quotes to represent backticks in this example)

'''{asis echo x <= 0.5}
# Interpretation #1
Based on the results, your X is too low...
'''
'''{asis echo x > 0.5}
# Interpretation #2
Based on the results, your X is too high
'''

Continuing with this example, if x came out to be 0.75, only interpretation #2 to appear in the HTML report that is generated. The code chunk is not python, but just regular markdown.

I've tried wrapping each possible markdown interpretation in separate code chunks and including either echo = False or evaluate = False as code chunk options, but that doesn't appear to do what I want it do. Instead of controlling whether or not the chunk is rendered, it just prints out at the beginning of the chunk.

'''{echo=False}# Interpretation #1
'''

How can I conditionally control the markdown that appears in the HTML report that is generated?

piccolbo commented 6 years ago

I think you need to use chunk option results = "raw" and produce the interpretations as python output. Please ignore the "\" character wherever it occurs. There is an empty line in the output that I am not sure how to eliminate.

\```python echo=False
x = 1
\```

Your x is too
\```python results='raw', echo=False
if x > 0:
  print ("high")
else:
  print ("low")
\```
piccolbo commented 6 years ago

I mean, there are several ways to eliminate the newline, but I would like to do the same thing directly inline, to minimize the code part. That may or may not matter to you.

piccolbo commented 6 years ago

Like this:

\```python echo=False
x = 1
\```

Your x is too <%print("high" if x>0 else "low")%>
CurtLH commented 6 years ago

Thanks for responding, but I think our situations are slightly different. I want to conditionally display a mardown chunk with headers, bullet points, bold text, ect, and I want control which markdown chunk appears based on some value of x.

For example, if x is less than 0.50, display the following markdown chunk:

You are really smart

But if x is greater than 0.50, display this other markdown chunk:

You are really really smart

piccolbo commented 6 years ago

Not sure what your last question really adds to the problem. Of course you can put markdown in the print statements. Maybe I am missing the point.

\```python echo=False
x = 0
\```

# You are really <% print("really" if x > 0.50 else "")%> smart

\```python results='raw', echo=False
if x < 0.50:
  print (
  """
-  bullet point number 1
-  bullet point number 2
-  bullet point number 3
  """)
else:
  print (
  """
-  bullet point 9
-  bullet point 8
-  bullet point 7
  """)
\```

Does that come close enough?

mpastell commented 6 years ago

There is no asis chunk type in Pweave. You can use workaround that @piccolbo showed.

A pull request adding this feature with documentation and tests would be welcome.

CurtLH commented 6 years ago

Thanks for the info. I'll go with the workaround @piccolbo mentioned for now, but will look into possibly submitting a PR to add the feature.

CurtLH commented 6 years ago

I tried your example @piccolbo, but the bullet point section is not being rendered as markdown. Instead, the output put all the bullet points on one line (example below).

You are really smart

- bullet point number 1 - bullet point number 2 - bullet point number 3

Instead, I want to output to render as markdown.

You are really smart

piccolbo commented 6 years ago

Did you check the markdown that was generated and do you find any fault with it? It works for me. How are you doing the rendering?

CurtLH commented 6 years ago

When I use the following command, the pweave -f markdown report.pmd, the outputs is as I would expect. However, when I use pweave -f md2html report.pmd, the markdown within the code chunk does not render as proper markdown.

Ultimately, I want to get to an HTML document, so I guess that I could go from pmd -> markdown -> HTML. However, maybe there is a more straight forward way to do this.

piccolbo commented 6 years ago

I think it's a bug in pweave. I tried to add the same markdown inside and outside the code block as in

\```python echo=False
x = 0
\```

# You are really <% print("really" if x > 0.50 else "")%> smart

\```python results='raw', echo=False
if x < 0.50:
  print (
  """
-  bullet point number 1
-  bullet point number 2
-  bullet point number 3
  """)
else:
  print (
  """
-  bullet point 9
-  bullet point 8
-  bullet point 7
  """)
\```

-  bullet point number 1
-  bullet point number 2
-  bullet point number 3

The generated markdown is the same:


# You are really  smart

-  bullet point number 1
-  bullet point number 2
-  bullet point number 3

-  bullet point number 1
-  bullet point number 2
-  bullet point number 3

But with md2html or pandoc2html only the second becomes a list. Strange, I thought any conversion would go through markdown as an intermediate format. Your workaround makes sense, but best would be to get to the bottom of this.

mpastell commented 6 years ago

md2html uses python-markdown instead of pandoc. You could try adding empty line after the list and see if that helps.

CurtLH commented 6 years ago

I tried your suggestion of adding an empty line after the list, and that didn't seem to make any difference.