How do i create Palette UI object
To use the UILayout.template_palette you need data that has a property of type bpy.types.Palette

Here is a small test to add a new custom palette, assign it a couple of colors, yellow and red, set red as active.

The image paint tool settings palette property is assigned the new palette.

The palette is displayed in the image editor toolbar under the "misc" tab.



import bpy

class PaletteDemoPanel(bpy.types.Panel):
    """Creates a Panel in the tool panel of image editor"""
    bl_label = "Palette Demo"
    bl_idname = "IMAGE_PT_palette"
    bl_space_type = 'IMAGE_EDITOR'
    bl_region_type = 'TOOLS'

    def draw(self, context):
        layout = self.layout

        ts = context.tool_settings
        if ts.image_paint.palette:
            layout.template_palette(ts.image_paint, "palette", color=True)


def register():
    bpy.utils.register_class(PaletteDemoPanel)


def unregister():
    bpy.utils.unregister_class(PaletteDemoPanel)


if __name__ == "__main__":
    register()

    # put some test code here to create palette etc
    # add a new pallete
    pal = bpy.data.palettes.get("CustomPalette")
    if pal is None:
        pal = bpy.data.palettes.new("CustomPalette")
        # add a color to that palette
        yellow = pal.colors.new()
        yellow.color = (1, 1, 0)
        yellow.weight = 1.0

        red = pal.colors.new()
        red.color = (1, 0, 0)

        # make red active
        pal.colors.active = red

    ts = bpy.context.tool_settings   
    ts.image_paint.palette = pal