textworld.render

class textworld.render.render.GraphItem(type, name)[source]

Bases: object

add_content(content)[source]
add_unknown_predicate(predicate)[source]
get_max_depth()[source]

Returns the maximum nest depth of this plus all children. A container with no items has 1 depth, a container containing one item has 2 depth, a container containing a container which contains an item has 3 depth, and so on. :return: maximum nest depth

set_open_closed_locked(status)[source]
to_dict()[source]
infos
class textworld.render.render.GraphRoom(name, base_room)[source]

Bases: object

add_item(item)[source]
Return type:None
position_string()[source]
Return type:str
textworld.render.render.concat_images(*images)[source]
textworld.render.render.load_state(world, game_infos=None, action=None, format='png', limit_player_view=False)[source]

Generates serialization of game state.

Parameters:
  • world (World) – The current state of the world to visualize.
  • game_infos (Optional[Dict[str, EntityInfo]]) – The mapping needed to get objects names.
  • action (Optional[Action]) – If provided, highlight the world changes made by that action.
  • format (str) – The graph output format (gv, svg, png…)
  • limit_player_view (bool) – Whether to limit the player’s view (defaults to false)
Return type:

dict

Returns:

The graph generated from this World

textworld.render.render.load_state_from_game_state(game_state, format='png', limit_player_view=False)[source]

Generates serialization of game state.

Parameters:
  • game_state (GlulxGameState) – The current game state to visualize.
  • format (str) – The graph output format (png, svg, pdf, …)
  • limit_player_view (bool) – Whether to limit the player’s view. Default: False.
Return type:

dict

Returns:

The graph generated from this World

textworld.render.render.take_screenshot(url, id='world')[source]

Takes a screenshot of DOM element given its id. :type url: str :param url: URL of webpage to open headlessly. :type id: str :param id: ID of DOM element. :return: Image object.

textworld.render.render.temp_viz(nodes, edges, pos, color=[])[source]
textworld.render.render.visualize(world, interactive=False)[source]

Show the current state of the world. :type world: Union[Game, State, GlulxGameState, World] :param world: Object representing a game state to be visualized. :type interactive: bool :param interactive: Whether or not to visualize the state in the browser. :return: Image object of the visualization.

Creates server for streamed game state

class textworld.render.serve.Server(game_state, port)[source]

Bases: object

Visualization server. Uses Server-sent Events to update game_state for visualization.

Note: Flask routes are defined in app.add_url_rule in order to call self in routes. :type game_state: dict :param game_state: game state returned from load_state_from_game_state :type port: int :param port: port to run visualization on

gen()[source]

Our generator for listening for updating state. We poll for results to return us something. If nothing is returned then we just pass and keep polling. :return: yields event-stream parsed data.

index()[source]

Index route (“/”). Returns HTML template processed by handlebars. :rtype: str :return: Flask response object

static listen(conn, results)[source]

Listener for updates. Runs on separate thread. :type conn: Connection :param conn: child connection from multiprocessing.Pipe. :type results: Queue :param results: thread-safe queue for results.

start(child_conn)[source]

Starts the WSGI server and listen for updates on a separate thread.

Parameters:child_conn (Connection) – Child connection from multiprocessing.Pipe.
subscribe()[source]

Our Server-sent Event stream route. :return: A stream

update_subscribers(game_state)[source]

Updates all subscribers and updates their data. This is for multiple subscribers on the visualization service. :type game_state: dict :param game_state: parsed game_state from load_state_from_game_state

class textworld.render.serve.ServerSentEvent(data)[source]

Bases: object

Object helper to parse dict into SSE data. :type data: <built-in function any> :param data: data to pass to SSE

encode()[source]
class textworld.render.serve.SupressStdStreams[source]

Bases: object

for surpressing std.out streams

class textworld.render.serve.VisualizationService(game_state, open_automatically)[source]

Bases: object

Server for visualization.

We instantiate a new process for our flask server, so our game can send updates to the server. The server instantiates new gevent Queues for every connection.

start(parent_thread, port)[source]

Start visualization server on a new process. :type parent_thread: Thread :param parent_thread: the parent thread that called start. :type port: int :param port: Port to run visualization on.

Return type:None
start_server(game_state, port, child_conn)[source]

function for starting new server on new process. :type game_state: dict :param game_state: initial game state from load :type port: int :param port: port to run server :type child_conn: Connection :param child_conn: child connection from multiprocessing.Pipe

stop_server()[source]
update_state(game_state, command)[source]

Propogate state update to server. We use a multiprocessing.Pipe to pass state into flask process. :type game_state: GlulxGameState :param game_state: Glulx game state. :type command: str :param command: previous command

textworld.render.serve.find_free_port(port_range)[source]
textworld.render.serve.get_html_template(game_state=None)[source]