Class: QgsLayoutItemHtml

A layout multiframe subclass for HTML content.

Class Hierarchy

Inheritance diagram of qgis.core.QgsLayoutItemHtml

Base classes

QgsLayoutMultiFrame

Abstract base class for layout items with the ability to distribute the content to several frames (QgsLayoutFrame items).

QgsLayoutObject

A base class for objects which belong to a layout.

QObject

QgsExpressionContextGenerator

Abstract interface for generating an expression context.

QgsLayoutUndoObjectInterface

Interface for layout objects which support undo/redo commands.

Methods

contentMode

Returns the source mode for item's HTML content.

evaluateExpressions

Returns whether html item will evaluate QGIS expressions prior to rendering the HTML content.

html

Returns the HTML source displayed in the item if the item is using the QgsLayoutItemHtml.ManualHtml mode.

loadHtml

Reloads the html source from the url and redraws the item.

maxBreakDistance

Returns the maximum distance allowed when calculating where to place page breaks in the html.

setContentMode

Sets the source mode for item's HTML content.

setEvaluateExpressions

Sets whether the html item will evaluate QGIS expressions prior to rendering the HTML content.

setHtml

Sets the html to display in the item when the item is using the QgsLayoutItemHtml.ManualHtml mode.

setMaxBreakDistance

Sets the maximum distance allowed when calculating where to place page breaks in the html.

setUrl

Sets the url for content to display in the item when the item is using the QgsLayoutItemHtml.Url mode.

setUseSmartBreaks

Sets whether the html item should use smart breaks.

setUserStylesheet

Sets the user stylesheet CSS rules to use while rendering the HTML content.

setUserStylesheetEnabled

Sets whether user stylesheets are enabled for the HTML content.

url

Returns the URL of the content displayed in the item if the item is using the QgsLayoutItemHtml.Url mode.

useSmartBreaks

Returns whether html item is using smart breaks.

userStylesheet

Returns the user stylesheet CSS rules used while rendering the HTML content.

userStylesheetEnabled

Returns whether user stylesheets are enabled for the HTML content.

Virtual Methods

In PyQGIS, only methods marked as virtual can be safely overridden in a Python subclass of QgsLayoutItemHtml. See the FAQ for more details.

recalculateFrameSizes

Recalculates the frame sizes for the current viewport dimensions

Static Methods

create

Returns a new QgsLayoutItemHtml for the specified parent layout.

createFromLabel

Returns a new QgsLayoutItemHtml matching the content and rendering of a given label.

Attributes

ManualHtml

Url

class qgis.core.QgsLayoutItemHtml[source]

Bases: QgsLayoutMultiFrame

__init__(layout: QgsLayout | None)

Constructor for QgsLayoutItemHtml, with the specified parent layout.

Ownership is transferred to the layout.

Parameters:

layout (Optional[QgsLayout])

class ContentMode

Bases: int

ManualHtml = 1
Url = 0
contentMode(self) QgsLayoutItemHtml.ContentMode[source]

Returns the source mode for item’s HTML content.

See also

setContentMode()

See also

url()

See also

html()

Return type:

QgsLayoutItemHtml.ContentMode

static create(layout: QgsLayout | None) QgsLayoutItemHtml | None[source]

Returns a new QgsLayoutItemHtml for the specified parent layout.

Parameters:

layout (Optional[QgsLayout])

Return type:

Optional[QgsLayoutItemHtml]

static createFromLabel(label: QgsLayoutItemLabel | None) QgsLayoutItemHtml | None[source]

Returns a new QgsLayoutItemHtml matching the content and rendering of a given label.

Added in version 3.32.

Parameters:

label (Optional[QgsLayoutItemLabel])

Return type:

Optional[QgsLayoutItemHtml]

evaluateExpressions(self) bool[source]

Returns whether html item will evaluate QGIS expressions prior to rendering the HTML content. If set, any content inside [% %] tags will be treated as a QGIS expression and evaluated against the current atlas feature.

Return type:

bool

html(self) str[source]

Returns the HTML source displayed in the item if the item is using the QgsLayoutItemHtml.ManualHtml mode.

See also

setHtml()

See also

contentMode()

Return type:

str

loadHtml(self, useCache: bool = False, context: QgsExpressionContext | None = None)[source]

Reloads the html source from the url and redraws the item.

Parameters:
  • useCache (bool = False) – set to True to use a cached copy of remote html content

  • context (Optional[QgsExpressionContext] = None) – expression context for evaluating data defined urls and expressions in html

See also

setUrl()

See also

url()

maxBreakDistance(self) float[source]

Returns the maximum distance allowed when calculating where to place page breaks in the html. This distance is the maximum amount of empty space allowed at the bottom of a frame after calculating the optimum break location. This setting is only effective if useSmartBreaks is True.

See also

useSmartBreaks()

Return type:

float

virtual recalculateFrameSizes(self)[source]

Recalculates the frame sizes for the current viewport dimensions

setContentMode(self, mode: QgsLayoutItemHtml.ContentMode)[source]

Sets the source mode for item’s HTML content.

See also

contentMode()

See also

setUrl()

See also

setHtml()

Parameters:

mode (QgsLayoutItemHtml.ContentMode)

setEvaluateExpressions(self, evaluateExpressions: bool)[source]

Sets whether the html item will evaluate QGIS expressions prior to rendering the HTML content. If set, any content inside [% %] tags will be treated as a QGIS expression and evaluated against the current atlas feature.

Parameters:

evaluateExpressions (bool)

setHtml(self, html: str | None)[source]

Sets the html to display in the item when the item is using the QgsLayoutItemHtml.ManualHtml mode. Setting the HTML using this function does not automatically refresh the item’s contents. Call loadHtml to trigger a refresh of the item after setting the HTML content.

See also

html()

See also

contentMode()

See also

loadHtml()

Parameters:

html (Optional[str])

setMaxBreakDistance(self, distance: float)[source]

Sets the maximum distance allowed when calculating where to place page breaks in the html. This distance is the maximum amount of empty space allowed at the bottom of a frame after calculating the optimum break location. Setting a larger value will result in better choice of page break location, but more wasted space at the bottom of frames. This setting is only effective if useSmartBreaks is True.

Parameters:

distance (float)

setUrl(self, url: QUrl)[source]

Sets the url for content to display in the item when the item is using the QgsLayoutItemHtml.Url mode. Content is automatically fetched and the HTML item refreshed after calling this function.

See also

url()

See also

contentMode()

Parameters:

url (QUrl)

setUseSmartBreaks(self, useSmartBreaks: bool)[source]

Sets whether the html item should use smart breaks. Smart breaks prevent the html frame contents from breaking mid-way though a line of text.

See also

useSmartBreaks()

Parameters:

useSmartBreaks (bool)

setUserStylesheet(self, stylesheet: str | None)[source]

Sets the user stylesheet CSS rules to use while rendering the HTML content. These allow for overriding the styles specified within the HTML source. Setting the stylesheet using this function does not automatically refresh the item’s contents. Call loadHtml to trigger a refresh of the item after setting the stylesheet rules.

See also

userStylesheet()

See also

loadHtml()

Parameters:

stylesheet (Optional[str])

setUserStylesheetEnabled(self, enabled: bool)[source]

Sets whether user stylesheets are enabled for the HTML content.

Parameters:

enabled (bool)

url(self) QUrl[source]

Returns the URL of the content displayed in the item if the item is using the QgsLayoutItemHtml.Url mode.

See also

setUrl()

See also

contentMode()

Return type:

QUrl

useSmartBreaks(self) bool[source]

Returns whether html item is using smart breaks. Smart breaks prevent the html frame contents from breaking mid-way though a line of text.

Return type:

bool

userStylesheet(self) str[source]

Returns the user stylesheet CSS rules used while rendering the HTML content. These overriding the styles specified within the HTML source.

Return type:

str

userStylesheetEnabled(self) bool[source]

Returns whether user stylesheets are enabled for the HTML content.

See also

userStylesheet()

Return type:

bool