Display a code block with optional syntax highlighting.

Method Signatures and Parameters

Jt.code(String body)

body (String)

The string to display as code or monospace text.

Chainable builder methods

language(String language)

The language that the code is written in, for syntax highlighting. This defaults to java. If this is null, the code will be plain, monospace text.

For a list of available language values, see list on prismjs documentation.

lineNumbers(boolean lineNumbers)

An optional boolean indicating whether to show line numbers to the left of the code block. This defaults to false.

wrapLines(boolean wrapLines)

An optional boolean indicating whether to wrap lines. This defaults to false.

height(String height)

The height of the code block element. This can be one of the following:

  • content (default): The height of the element matches the height of its content.
  • stretch: The height of the element matches the height of its content or the height of the parent container, whichever is larger. If the element is not in a parent container, the height of the element matches the height of its content.
  • An integer specifying the height in pixels: The element has a fixed height. If the content is larger than the specified height, scrolling is enabled.

height(int heightPixels)

The height of the element in pixels. The element will have a fixed height. If the content is larger than the specified height, scrolling is enabled.

width(String width)

The width of the element. This can be one of the following:

  • content (default): The width of the element matches the width of its content, but doesn't exceed the width of the parent container.
  • stretch: The width of the element matches the width of the parent container.
  • An integer specifying the width in pixels: The element has a fixed width. If the specified width is greater than the width of the parent container, the width of the element matches the width of the parent container.

width(int widthPixels)

The width of the element in pixels. The element will have a fixed width. If the specified width is greater than the width of the parent container, the width of the element matches the width of the parent container.

key(String key)

A string to use as the unique key for the widget. If this is omitted, a key will be generated for the widget based on its content. No two widgets may have the same key.

No description

use()

Put the widget in the app, in the MAIN container.

use(JtContainer container)

Put the widget in the app, in the provided container.

Examples

Simple code block

 import tech.catheu.jeamlit.core.Jt;

 public class CodeApp {
     public static void main(String[] args) {
         Jt.code("public class HelloWorld {}").use();
     }
 }

Multi-line code with syntax highlighting

 import tech.catheu.jeamlit.core.Jt;

 public class MultilineCodeApp {
     public static void main(String[] args) {
         String pythonCode = """
                 import numpy as np

                 a = np.arange(15).reshape(3, 5)
                 """;
         Jt.code(pythonCode).language("python").use();
     }
 }

forum

Still have questions?

Go to our discussions forum for helpful information and advice from Jeamlit experts.