Search code examples
kivyrestructuredtextwxpython

best tool for rendering short items of text in wxPython and Kivi


I have a multi-platform app running on desktop (wxPython) and mobile (kivy). In it I want to render small areas of variable text in a window in the app. The text will depend on the state of the app. I am happy to use rtf, html or reStructuredText. I need to use the same source for the text on each platform.

A typical example of a text snippet would be:

Heading
=======

1. With 24 widgets pull a **long** one;
2. with fewer, push a **wide** one.

Which would render as:


Heading

  1. With 24 widgets pull a long one;
  2. with fewer, push a wide one.

My question is: which format should I use?

My preference would be reStructuredText. There appears to be a kivy widget to support this but nothing in wxPython


Solution

  • One solution is to use the docutils package.

    This will take reStructuredText and output it as html. I can then use the wxPython wx.html control to display the output.

    import wx
    import wx.html as wxhtml
    from docutils.core import publish_string
    
    
    
    class MainFrame(wx.Frame):
        def __init__(self, *args, **kwargs):
            wx.Frame.__init__(self, *args, **kwargs)
            self.panel = MainPanel(self)
            sizer = wx.BoxSizer()
            sizer.Add(self.panel)
            self.SetSizerAndFit(sizer)
            self.Show()
    
            input_string = ("Heading\n"
                            "=======\n"
                            "\n"
                            "1. With 24 widgets pull a **long** one;\n"
                            "2. with fewer, push a **wide** one.\n")
            self.display_rst(input_string)
    
        def display_rst(self, rst):
            html = publish_string(rst, writer_name='html')
            self.panel.html.SetPage(html)
    
    class MainPanel(wx.Panel):
        def __init__(self, parent, *args, **kwargs):
            wx.Panel.__init__(self, parent, *args, **kwargs)
            display_style = wx.VSCROLL|wx.HSCROLL|wx.TE_READONLY|wx.BORDER_SIMPLE
            self.html = wxhtml.HtmlWindow(self, -1, size=(300, 200),
                                        style=display_style)
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.Add(self.html)
            self.SetSizer(sizer)
    
    if __name__ == "__main__":
        SCREEN_APP = wx.App()
        MAIN_FRAME = MainFrame(None, title="Display HTML")
        SCREEN_APP.MainLoop()