jankatins / knitpy

knitpy: Elegant, flexible and fast dynamic report generation with python
https://github.com/jankatins/knitpy
Other
368 stars 29 forks source link

inline code string values rendering as unicode python, eg u'some string' instead of #27

Closed thatcher closed 3 years ago

thatcher commented 9 years ago

In a .pymd this is the mark up

My name is `python my_name`

renders as

My name is u'Chris'

instead of

My name is Chris

jankatins commented 9 years ago

I'm not sure that there is an easy fix here :-( The current code behind this is basically the same as writing this in a cell in the notebook:

In [5]: u"üä"
Out[5]: u'\xfc\xe4'

So, if the above inline code should return the string without quotes (and as a normal "readable" unicode instead of encoded ones) but in code blocks this should show quotes and u"..." signs... this needs to switch the plain text formatter for unicode/str before/after each submitted code (or at least when switching from inline to block and reverse) :-/

from IPython.core.interactiveshell import InteractiveShell
shell = InteractiveShell.instance()
tp = shell.display_formatter.formatters[u'text/plain']
def pp(x,p,z):
    p.text(x)
tp.type_printers[unicode] = pp
tp.type_printers[str] = pp

Not sure if there are any better alternatives...

jankatins commented 8 years ago

Or the easier way: add a small wrapper in the engine code which does this for you:

engine would then need to setup p (or better a function with a unique and hidden name, which imports the right things and submits the final value to the ipython formatter...) in the engine setup and needs a configureable string which is used as the template, which should surround the inline code

So instead of submitting my_name it's submitting _kpf(my_name) to the kernel...