API Reference

class turbo_flask.Turbo(app=None)
turbo(version='8.0.11', url=None)

Add turbo.js to the page.

This method is accessible in template files as turbo. You must add {{ turbo() }} in the <head> section of your main template to activate turbo.js.

Parameters:
  • version – the version of turbo.js to load.

  • url – The URL for the turbo.js library, or None to use the default version and CDN.

user_id(f)

Configure an application-specific user id generator, to allow the application to push updates over WebSocket to individual clients.

Example:

@turbo.user_id
def get_user_id():
    return current_user.id
default_user_id()

Default user id generator. An application-specific function can be configured with the @user_id decorator.

requested_frame()

Returns the target frame the client expects, or None.

can_stream()

Returns True if the client accepts turbo stream reponses.

can_push(to=None)

Returns True if the client accepts turbo stream updates over WebSocket.

Parameters:

to – the id of the client. If not given then the answer is True if there is at least one client listening to updates over WebSocket.

append(content, target, multiple=False)

Create an append stream.

Parameters:
  • content – the HTML content to include in the stream.

  • target – the target ID or CSS query selector for this change.

  • multiple – set to True when target references multiple elements.

prepend(content, target, multiple=False)

Create a prepend stream.

Parameters:
  • content – the HTML content to include in the stream.

  • target – the target ID or CSS query selector for this change.

  • multiple – set to True when target references multiple elements.

replace(content, target, multiple=False)

Create a replace stream.

Parameters:
  • content – the HTML content to include in the stream.

  • target – the target ID or CSS query selector for this change.

  • multiple – set to True when target references multiple elements.

update(content, target, multiple=False)

Create an update stream.

Parameters:
  • content – the HTML content to include in the stream.

  • target – the target ID or CSS query selector for this change.

  • multiple – set to True when target references multiple elements.

remove(target, multiple=False)

Create a remove stream.

Parameters:
  • target – the target ID or CSS query selector for this change.

  • multiple – set to True when target references multiple elements.

after(content, target, multiple=False)

Create an after stream.

Parameters:
  • content – the HTML content to include in the stream.

  • target – the target ID or CSS query selector for this change.

  • multiple – set to True when target references multiple elements.

before(content, target, multiple=False)

Create an before stream.

Parameters:
  • content – the HTML content to include in the stream.

  • target – the target ID or CSS query selector for this change.

  • multiple – set to True when target references multiple elements.

stream(stream)

Create a turbo stream response.

Parameters:

stream – one or a list of streamed responses generated by the append(), prepend(), replace(), update() and remove() methods.

push(stream, to=None)

Push a turbo stream update over WebSocket to one or more clients.

Parameters:
  • stream – one or a list of stream updates generated by the append(), prepend(), replace(), update() and remove() methods.

  • to – the id of the target client. Set to None to send to all connected clients, or to a list of ids to target multiple clients.