View the corresponding python.Rmd file to learn more about how this page was generated.

library(knitractive)
library(rexpect)
library(rlang)
start(name = "python",
      command = cmd_docker(command = "python3",
                           image = "continuumio/anaconda3",
                           volume = list2(!!getwd() := "/data")),
      prompt = prompts$python,
      keep_session = FALSE)

In this example, we're using the continuumio/anaconda3 docker image to run Python.

import numpy as np
import pandas as pd
import matplotlib
Enter#! literal = FALSE
matplotlib.use('agg')
pd.__version__

Because knitractive employs the tmuxr package under the hood, the python session is maintained, and we can continue where we left off.

ts = pd.DataFrame({"value": np.random.randn(1000)},
                  index=pd.date_range('1/1/2000',
                  periods=1000))
ts = ts.cumsum()
ts.head()#! enter = FALSE, expect_prompt = FALSE

```{python, include = FALSE} C-c#! literal = FALSE, expect_prompt = TRUE ts.head().to_html("/data/python_ts.html", border=0)

```r
htmltools::includeHTML("python_ts.html")
unlink("python_ts.html")

We've mapped the current working directory on the host to the directory /data/ inside the docker container, so that we can include generated figures.

ts.plot() #! enter = FALSE, expect_prompt = FALSE

```{python, include = FALSE} C-c #! wait = 0.2, literal = FALSE, expect_prompt = TRUE ts.plot().figure.savefig("/data/python_ts.png", dpi = 200)

```r
knitr::include_graphics(paste0(getwd(), "/python_ts.png"))

View the corresponding python.Rmd file to learn more about how this page was generated.



datascienceworkshops/knitractive documentation built on Nov. 17, 2021, 7:50 p.m.