Queue a new cell. Returns the new cell's ID. The returned ID can be used in subsequent operations within the same batch (e.g. as an `after` target for the next cell). Cells are not executed automatically. Use `run_cell` to queue them for execution:: cid
(
self,
code: str,
*,
before: str | None = None,
after: str | None = None,
hide_code: bool = True,
disabled: bool = False,
column: int | None = None,
name: str | None = None,
)
| 1035 | return cell_id, name |
| 1036 | |
| 1037 | def create_cell( |
| 1038 | self, |
| 1039 | code: str, |
| 1040 | *, |
| 1041 | before: str | None = None, |
| 1042 | after: str | None = None, |
| 1043 | hide_code: bool = True, |
| 1044 | disabled: bool = False, |
| 1045 | column: int | None = None, |
| 1046 | name: str | None = None, |
| 1047 | ) -> CellId_t: |
| 1048 | """Queue a new cell. Returns the new cell's ID. |
| 1049 | |
| 1050 | The returned ID can be used in subsequent operations within the |
| 1051 | same batch (e.g. as an `after` target for the next cell). |
| 1052 | |
| 1053 | Cells are not executed automatically. Use `run_cell` to queue |
| 1054 | them for execution:: |
| 1055 | |
| 1056 | cid = ctx.create_cell("x = 1") |
| 1057 | ctx.run_cell(cid) |
| 1058 | |
| 1059 | Examples: |
| 1060 | ```python |
| 1061 | # Append at the end |
| 1062 | cid = ctx.create_cell("import pandas as pd") |
| 1063 | |
| 1064 | # Chain cells in order |
| 1065 | cid2 = ctx.create_cell("df = pd.read_csv('data.csv')", after=cid) |
| 1066 | ctx.create_cell("df.head()", after=cid2) |
| 1067 | |
| 1068 | # Create and run |
| 1069 | cid = ctx.create_cell("x = 1") |
| 1070 | ctx.run_cell(cid) |
| 1071 | ``` |
| 1072 | |
| 1073 | Args: |
| 1074 | code (str): Python source code for the cell. |
| 1075 | before (str, optional): Insert before this cell (ID or name). |
| 1076 | Mutually exclusive with `after`. |
| 1077 | after (str, optional): Insert after this cell (ID or name). |
| 1078 | Mutually exclusive with `before`. |
| 1079 | hide_code (bool): Collapse the code editor in the UI. |
| 1080 | Defaults to True. |
| 1081 | disabled (bool): Prevent the cell from executing. |
| 1082 | Defaults to False. |
| 1083 | column (int, optional): Column index for multi-column layouts. |
| 1084 | name (str, optional): Cell names are a human-facing label, |
| 1085 | reserved for special cases (e.g. `"setup"`). Prefer |
| 1086 | referencing cells by the returned cell ID unless |
| 1087 | naming is important for the user. |
| 1088 | """ |
| 1089 | self._require_entered() |
| 1090 | if before is not None and after is not None: |
| 1091 | raise ValueError("Cannot specify both 'before' and 'after'") |
| 1092 | |
| 1093 | cell_id, resolved_name = self._resolve_new_cell(name) |
| 1094 |