From b9aefe36c54f07723e100813c3d8755ef80cca6d Mon Sep 17 00:00:00 2001 From: edavidaja Date: Fri, 2 May 2025 15:11:35 -0400 Subject: [PATCH 1/4] add comprehensive CLI documentation --- README.md | 1209 +--------------- CHANGELOG.md => docs/CHANGELOG.md | 0 docs/README.md | 30 - docs/commands/add.md | 3 + docs/commands/bootstrap.md | 3 + docs/commands/content.md | 3 + docs/commands/deploy.md | 3 + docs/commands/details.md | 3 + docs/commands/info.md | 3 + docs/commands/list.md | 3 + docs/commands/remove.md | 3 + docs/commands/system.md | 3 + docs/commands/version.md | 3 + docs/commands/write-manifest.md | 3 + docs/{docs => }/css/custom.css | 0 docs/{docs => }/images/favicon.ico | Bin docs/{docs => }/images/iconPositConnect.svg | 0 .../images/posit-logo-fullcolor-TM.svg | 0 docs/{docs => }/images/positLogoBlack.svg | 0 docs/{docs => }/images/positLogoWhite.svg | 0 docs/{docs => }/images/rstudio-logo.png | Bin docs/index.md | 1218 +++++++++++++++++ docs/mkdocs.yml | 55 - mkdocs.yml | 87 ++ pyproject.toml | 6 + 25 files changed, 1362 insertions(+), 1276 deletions(-) rename CHANGELOG.md => docs/CHANGELOG.md (100%) delete mode 100644 docs/README.md create mode 100644 docs/commands/add.md create mode 100644 docs/commands/bootstrap.md create mode 100644 docs/commands/content.md create mode 100644 docs/commands/deploy.md create mode 100644 docs/commands/details.md create mode 100644 docs/commands/info.md create mode 100644 docs/commands/list.md create mode 100644 docs/commands/remove.md create mode 100644 docs/commands/system.md create mode 100644 docs/commands/version.md create mode 100644 docs/commands/write-manifest.md rename docs/{docs => }/css/custom.css (100%) rename docs/{docs => }/images/favicon.ico (100%) rename docs/{docs => }/images/iconPositConnect.svg (100%) rename docs/{docs => }/images/posit-logo-fullcolor-TM.svg (100%) rename docs/{docs => }/images/positLogoBlack.svg (100%) rename docs/{docs => }/images/positLogoWhite.svg (100%) rename docs/{docs => }/images/rstudio-logo.png (100%) create mode 100644 docs/index.md delete mode 100644 docs/mkdocs.yml create mode 100644 mkdocs.yml diff --git a/README.md b/README.md index 24dc83c2..7d6a2502 100644 --- a/README.md +++ b/README.md @@ -1,1218 +1,45 @@ -# The rsconnect-python CLI +# [rsconnect-python](https://docs.posit.co/rsconnect-python) -This package provides a CLI (command-line interface) for interacting -with and deploying to Posit Connect. Many types of content supported by Posit -Connect may be deployed by this package, including WSGI-style APIs, Dash, Streamlit, -Gradio, and Bokeh applications. +The [Posit Connect](https://docs.posit.co/connect/) command-line interface. -Content types not directly supported by the CLI may also be deployed if they include a -prepared `manifest.json` file. See ["Deploying R or Other -Content"](#deploying-r-or-other-content) for details. +## Installation - -### Installation - -To install `rsconnect-python` from PYPI, you may use any python package manager such as -pip: - -```bash -pip install rsconnect-python -``` - -You may also build and install a wheel directly from a repository clone: - -```bash -git clone https://github.com/posit-dev/rsconnect-python.git -cd rsconnect-python -pip install pipenv -make dist -pip install ./dist/rsconnect_python-*.whl -``` - -### Using the rsconnect CLI - -Here's an example command that deploys a Jupyter notebook to Posit Connect. - -```bash -rsconnect deploy notebook \ - --server https://connect.example.org \ - --api-key my-api-key \ - my-notebook.ipynb -``` - -> **Note** -> The examples here use long command line options, but there are short -> options (`-s`, `-k`, etc.) available also. Run `rsconnect deploy notebook --help` -> for details. - -### Setting up `rsconnect` CLI auto-completion - -If you would like to use your shell's tab completion support with the `rsconnect` -command, use the command below for the shell you are using. - -#### `bash` - -If you are using the `bash` shell, use this to enable tab completion. - -```bash -#~/.bashrc -eval "$(_RSCONNECT_COMPLETE=source rsconnect)" -``` - -#### `zsh` - -If you are using the `zsh` shell, use this to enable tab completion. - -```zsh -#~/.zshrc -eval "$(_RSCONNECT_COMPLETE=source_zsh rsconnect)" -``` - -If you get `command not found: compdef`, you need to add the following lines to your -`.zshrc` before the completion setup: - -```zsh -#~/.zshrc -autoload -Uz compinit -compinit -``` - -### Managing Server Information - -The information used by the `rsconnect` command to communicate with a Posit Connect -server can be tedious to repeat on every command. To help, the CLI supports the idea -of saving this information, making it usable by a simple nickname. - -> **Warning** -> One item of information saved is the API key used to authenticate with -> Posit Connect. Although the file where this information is saved is marked as -> accessible by the owner only, it's important to remember that the key is present -> in the file as plain text so care must be taken to prevent any unauthorized access -> to the server information file. - -#### TLS Support and Posit Connect - -Usually, a Posit Connect server will be set up to be accessed in a secure manner, -using the `https` protocol rather than simple `http`. If Posit Connect is set up -with a self-signed certificate, you will need to include the `--insecure` flag on -all commands. If Posit Connect is set up to require a client-side certificate chain, -you will need to include the `--cacert` option that points to your certificate -authority (CA) trusted certificates file. Both of these options can be saved along -with the URL and API Key for a server. - -> **Note** -> When certificate information is saved for the server, the specified file -> is read and its _contents_ are saved under the server's nickname. If the CA file's -> contents are ever changed, you will need to add the server information again. - -See the [Network Options](#network-options) section for more details about these options. - -#### Remembering Server Information - -Use the `add` command to store information about a Posit Connect server: - -```bash -rsconnect add \ - --api-key my-api-key \ - --server https://connect.example.org \ - --name myserver -``` - -> **Note** -> The `rsconnect` CLI will verify that the serve URL and API key -> are valid. If either is found not to be, no information will be saved. - -If any of the access information for the server changes, simply rerun the -`add` command with the new information and it will replace the original -information. - -Once the server's information is saved, you can refer to it by its nickname: - -```bash -rsconnect deploy notebook --name myserver my-notebook.ipynb -``` - -If there is information for only one server saved, this will work too: - -```bash -rsconnect deploy notebook my-notebook.ipynb -``` - -#### Listing Server Information - -You can see the list of saved server information with: - -``` -rsconnect list -``` - -#### Removing Server Information - -You can remove information about a server with: - -``` -rsconnect remove --name myserver -``` - -Removing may be done by its nickname (`--name`) or URL (`--server`). - -### Verifying Server Information - -You can verify that a URL refers to a running instance of Posit Connect by using -the `details` command: - -```bash -rsconnect details --server https://connect.example.org -``` - -In this form, `rsconnect` will only tell you whether the URL given does, in fact, refer -to a running Posit Connect instance. If you include a valid API key: - -```bash -rsconnect details --server https://connect.example.org --api-key my-api-key -``` - -the tool will provide the version of Posit Connect (if the server is configured to -divulge that information) and environmental information including versions of Python -that are installed on the server. - -You can also use nicknames with the `details` command if you want to verify that the -stored information is still valid. - -### Notebook Deployment Options - -There are a variety of options available to you when deploying a Jupyter notebook to -Posit Connect. - -#### Including Extra Files - -You can include extra files in the deployment bundle to make them available when your -notebook is run by the Posit Connect server. Just specify them on the command line -after the notebook file: - -```bash -rsconnect deploy notebook my-notebook.ipynb data.csv -``` - -#### Package Dependencies - -If a `requirements.txt` file exists in the same directory as the notebook file, it will -be included in the bundle. It must specify the package dependencies needed to execute -the notebook. Posit Connect will reconstruct the Python environment using the -specified package list. - -If there is no `requirements.txt` file or the `--force-generate` option is specified, -the package dependencies will be determined from the current Python environment, or -from an alternative Python executable specified via the `--python` option: - -```bash -rsconnect deploy notebook --python /path/to/python my-notebook.ipynb -``` - -You can see the packages list that will be included by running `pip list --format=freeze` yourself, -ensuring that you use the same Python that you use to run your Jupyter Notebook: - -```bash -/path/to/python -m pip list --format=freeze -``` - -#### Python Version - -When deploying Python content to Posit Connect, -the server will require a version of Python that matches the content -requirements. - -For example, a server with only Python 3.9 installed will fail to match content -that requires Python 3.8. - -`rsconnect` supports detecting Python version requirements in several ways: - 1. A `.python-version` file exists. In such case - `rsconnect` will use its content to determine the python version requirement. - 2. A `pyproject.toml` with a `project.requires-python` field exists. - In such case the requirement specified in the field will be used - if no `.python-version` file exists. - 3. A `setup.cfg` with an `options.python_requires` field exists. - In such case the requirement specified in the field will be used - if **1** or **2** were not already satisfied. - 4. If no other source of version requirement was found, then - the interpreter in use is considered the one required to run the content. - -On Posit Connect `>=2025.03.0` the requirement detected by `rsconnect` is -always respected. Older Connect versions will instead rely only on the -python version used to deploy the content to determine the requirement. - -For more information see the [Posit Connect Admin Guide chapter titled Python Version -Matching](https://docs.posit.co/connect/admin/python/#python-version-matching). - -We recommend providing a `pyproject.toml` with a `project.requires-python` field -if the deployed content is an installable package and a `.python-version` file -for plain directories. - -> **Note** -> The packages and package versions listed in `requirements.txt` must be -> compatible with the Python version you request. - - -#### Static (Snapshot) Deployment - -By default, `rsconnect` deploys the original notebook with all its source code. This -enables the Posit Connect server to re-run the notebook upon request or on a schedule. - -If you just want to publish an HTML snapshot of the notebook, you can use the `--static` -option. This will cause `rsconnect` to execute your notebook locally to produce the HTML -file, then publish the HTML file to the Posit Connect server: - -```bash -rsconnect deploy notebook --static my-notebook.ipynb -``` - -### Creating a Manifest for Future Deployment - -You can create a `manifest.json` file for a Jupyter Notebook, then use that manifest -in a later deployment. Use the `write-manifest` command to do this. - -The `write-manifest` command will also create a `requirements.txt` file, if it does -not already exist or the `--force-generate` option is specified. It will contain the -package dependencies from the current Python environment, or from an alternative -Python executable specified in the `--python` option. - -Here is an example of the `write-manifest` command: - -```bash -rsconnect write-manifest notebook my-notebook.ipynb -``` - -> **Note** -> Manifests for static (pre-rendered) notebooks cannot be created. - -### API/Application Deployment Options - -You can deploy a variety of APIs and applications using sub-commands of the -`rsconnect deploy` command. - -* `api`: WSGI-compliant APIs (e.g., `bottle`, `falcon`, `flask`, `flask-restx`, `flasgger`, `pycnic`). -* `flask`: Flask APIs (_Note: `flask` is an alias of `api`._). -* `fastapi`: ASGI-compliant APIs (e.g, `fastapi`, `quart`, `sanic`, `starlette`) -* `dash`: Python Dash apps -* `streamlit`: Streamlit apps -* `bokeh`: Bokeh server apps -* `gradio`: Gradio apps - -All options below apply equally to the `api`, `fastapi`, `dash`, `streamlit`, -`gradio`, and `bokeh` sub-commands. - -#### Including Extra Files - -You can include extra files in the deployment bundle to make them available when your -API or application is run by the Posit Connect server. Just specify them on the -command line after the API or application directory: - -```bash -rsconnect deploy api flask-api/ data.csv -``` - -Since deploying an API or application starts at a directory level, there will be times -when some files under that directory subtree should not be included in the deployment -or manifest. Use the `--exclude` option to specify files or directories to exclude. - -```bash -rsconnect deploy dash --exclude dash-app-venv --exclude TODO.txt dash-app/ -``` - -You can exclude a directory by naming it: -```bash -rsconnect deploy dash --exclude dash-app-venv --exclude output/ dash-app/ -``` - -The `--exclude` option may be repeated, and may include a glob pattern. -You should always quote a glob pattern so that it will be passed to `rsconnect` as-is -instead of letting the shell expand it. If a file is specifically listed as an extra -file that also matches an exclusion pattern, the file will still be included in the -deployment (i.e., extra files take precedence). - -```bash -rsconnect deploy dash --exclude dash-app-venv --exclude “*.txt” dash-app/ -``` - -The following shows an example of an extra file taking precedence: - -```bash -rsconnect deploy dash --exclude “*.csv” dash-app/ important_data.csv -``` - -The "`**`" glob pattern will recursively match all files and directories, -while "`*`" only matches files. The "`**`" pattern is useful with complicated -project hierarchies where enumerating the _included_ files is simpler than -listing the _exclusions_. +### uv ```bash -rsconnect deploy quarto . _quarto.yml index.qmd requirements.txt --exclude "**" +uv tool install rsconnect-python ``` -Some directories are excluded by default, to prevent bundling and uploading files that are not needed or might interfere with the deployment process: - -``` -.Rproj.user -.env -.git -.svn -.venv -__pycache__ -env -packrat -renv -rsconnect-python -rsconnect -venv -``` - -Any directory that appears to be a Python virtual environment (by containing -`bin/python`) will also be excluded. - - -#### Package Dependencies - -If a `requirements.txt` file exists in the API/application directory, it will be -included in the bundle. It must specify the package dependencies needed to execute -the API or application. Posit Connect will reconstruct the Python environment using -the specified package list. - -If there is no `requirements.txt` file or the `--force-generate` option is specified, -the package dependencies will be determined from the current Python environment, or -from an alternative Python executable specified via the `--python` option: - -```bash -rsconnect deploy api --python /path/to/python my-api/ -``` - -You can see the packages list that will be included by running `pip list --format=freeze` yourself, -ensuring that you use the same Python that you use to run your API or application: +### pipx ```bash -/path/to/python -m pip list --format=freeze +pipx install rsconnect-python ``` -#### Python Version - -When deploying Python content to Posit Connect, -the server will require matching `` versions of Python. For example, -a server with only Python 3.9 installed will fail to match content deployed with -Python 3.8. Your administrator may also enable exact Python version matching which -will be stricter and require matching major, minor, and patch versions. For more -information see the [Posit Connect Admin Guide chapter titled Python Version -Matching](https://docs.posit.co/connect/admin/python/#python-version-matching). - -We recommend installing a version of Python on your client that is also available -in your Connect installation. If that's not possible, you can override -rsconnect-python's detected Python version and request a version of Python -that is installed in Connect, For example, this command: - -```bash -rsconnect deploy api --override-python-version 3.11.5 my-api/ -``` - -will deploy the content in `my-api` while requesting that Connect -use Python version 3.11.5. - -> **Note** -> The packages and package versions listed in `requirements.txt` must be -> compatible with the Python version you request. - -### Creating a Manifest for Future Deployment - -You can create a `manifest.json` file for an API or application, then use that -manifest in a later deployment. Use the `write-manifest` command to do this. - -The `write-manifest` command will also create a `requirements.txt` file, if it does -not already exist or the `--force-generate` option is specified. It will contain -the package dependencies from the current Python environment, or from an alternative -Python executable specified in the `--python` option. - -Here is an example of the `write-manifest` command: +### into your project ```bash -rsconnect write-manifest api my-api/ +python -m pip install rsconnect-python ``` -### Deploying R or Other Content +## Usage -You can deploy other content that has an existing Posit Connect `manifest.json` -file. For example, if you download and unpack a source bundle from Posit Connect, -you can deploy the resulting directory. The options are similar to notebook or -API/application deployment; see `rsconnect deploy manifest --help` for details. +Obtain an API key from your Posit Connect server with publisher privileges: -Here is an example of the `deploy manifest` command: +Store your credentials: ```bash -rsconnect deploy manifest /path/to/manifest.json +rsconnect add --server https://connect.example.com --api-key --name production ``` -> **Note** -> In this case, the existing content is deployed as-is. Python environment -> inspection and notebook pre-rendering, if needed, are assumed to be done already -> and represented in the manifest. +Deploy your application: -The argument to `deploy manifest` may also be a directory so long as that directory -contains a `manifest.json` file. - -If you have R content but don't have a `manifest.json` file, you can use the RStudio -IDE to create the manifest. See the help for the `rsconnect::writeManifest` R function: - -```r -install.packages('rsconnect') -library(rsconnect) -?rsconnect::writeManifest -``` - -### Options for All Types of Deployments - -These options apply to any type of content deployment. - -#### Title - -The title of the deployed content is, by default, derived from the filename. For -example, if you deploy `my-notebook.ipynb`, the title will be `my-notebook`. To change -this, use the `--title` option: - -``` -rsconnect deploy notebook --title "My Notebook" my-notebook.ipynb -``` - -When using `rsconnect deploy api`, `rsconnect deploy fastapi`, `rsconnect deploy dash`, -`rsconnect deploy streamlit`, `rsconnect deploy bokeh`, or `rsconnect deploy gradio`, -the title is derived from the directory containing the API or application. - -When using `rsconnect deploy manifest`, the title is derived from the primary -filename referenced in the manifest. - -#### Verification After Deployment -After deploying your content, rsconnect accesses the deployed content -to verify that the deployment is live. This is done with a `GET` request -to the content, without parameters. The request is -considered successful if there isn't a 5xx code returned. Errors like -400 Bad Request or 405 Method Not Allowed because not all apps support `GET /`. -For cases where this is not desired, use the `--no-verify` flag on the command line. - -### Environment variables -You can set environment variables during deployment. Their names and values will be -passed to Posit Connect during deployment so you can use them in your code. Note that -if you are using `rsconnect` to deploy to shinyapps.io, environment variable management -is not supported on that platform. - -For example, if `notebook.ipynb` contains -```python -print(os.environ["MYVAR"]) -``` - -You can set the value of `MYVAR` that will be set when your code runs in Posit Connect -using the `-E/--environment` option: ```bash -rsconnect deploy notebook --environment MYVAR='hello world' notebook.ipynb +rsconnect deploy shiny app.py --title "my shiny app" ``` -To avoid exposing sensitive values on the command line, you can specify -a variable without a value. In this case, it will use the value from the -environment in which rsconnect-python is running: -```bash -export SECRET_KEY=12345 - -rsconnect deploy notebook --environment SECRET_KEY notebook.ipynb -``` - -If you specify environment variables when updating an existing deployment, -new values will be set for the variables you provided. Other variables will -remain unchanged. If you don't specify any variables, all of the existing -variables will remain unchanged. - -Environment variables are set on the content item before the content bundle -is uploaded and deployed. If the deployment fails, the new environment variables -will still take effect. - -### Network Options +[Read more about publisher and admin capabilities on the docs site.](https://docs.posit.co/rsconnect-python) -When specifying information that `rsconnect` needs to be able to interact with Posit -Connect, you can tailor how transport layer security is performed. - -#### TLS/SSL Certificates - -Posit Connect servers can be configured to use TLS/SSL. If your server's certificate -is trusted by your Jupyter Notebook server, API client or user's browser, then you -don't need to do anything special. You can test this out with the `details` command: - -```bash -rsconnect details \ - --api-key my-api-key \ - --server https://connect.example.org:3939 -``` - -If this fails with a TLS Certificate Validation error, then you have two options. - -* Provide the Root CA certificate that is at the root of the signing chain for your - Posit Connect server. This will enable `rsconnect` to securely validate the - server's TLS certificate. - - ```bash - rsconnect details \ - --api-key my-api-key \ - --server https://connect.example.org \ - --cacert /path/to/certificate.pem - ``` - -* Posit Connect is in "insecure mode". This disables TLS certificate verification, - which results in a less secure connection. - - ```bash - rsconnect add \ - --api-key my-api-key \ - --server https://connect.example.org \ - --insecure - ``` - -Once you work out the combination of options that allow you to successfully work with -an instance of Posit Connect, you'll probably want to use the `add` command to have -`rsconnect` remember those options and allow you to just use a nickname. - -### Updating a Deployment - -If you deploy a file again to the same server, `rsconnect` will update the previous -deployment. This means that you can keep running `rsconnect deploy notebook my-notebook.ipynb` -as you develop new versions of your notebook. The same applies to other Python content -types. - -#### Forcing a New Deployment - -To bypass this behavior and force a new deployment, use the `--new` option: - -```bash -rsconnect deploy dash --new my-app/ -``` - -#### Updating a Different Deployment - -If you want to update an existing deployment but don't have the saved deployment data, -you can provide the app's numeric ID or GUID on the command line: - -```bash -rsconnect deploy notebook --app-id 123456 my-notebook.ipynb -``` - -You must be the owner of the target deployment, or a collaborator with permission to -change the content. The type of content (static notebook, notebook with source code, -API, or application) must match the existing deployment. - -> **Note** -> There is no confirmation required to update a deployment. If you do so -> accidentally, use the "Source Versions" dialog in the Posit Connect dashboard to -> activate the previous version and remove the erroneous one. - -##### Finding the App ID - -The App ID associated with a piece of content you have previously deployed from the -`rsconnect` command line interface can be found easily by querying the deployment -information using the `info` command. For more information, see the -[Showing the Deployment Information](#showing-the-deployment-information) section. - -If the content was deployed elsewhere or `info` does not return the correct App ID, -but you can open the content on Posit Connect, find the content and open it in a -browser. The URL in your browser's location bar will contain `#/apps/NNN` where `NNN` -is your App ID. The GUID identifier for the app may be found on the **Info** tab for -the content in the Posit Connect UI. - -#### Showing the Deployment Information - -You can see the information that the `rsconnect` command has saved for the most recent -deployment with the `info` command: - -```bash -rsconnect info my-notebook.ipynb -``` - -If you have deployed to multiple servers, the most recent deployment information for -each server will be shown. This command also displays the path to the file where the -deployment data is stored. - -## Stored Information Files - -Stored information files are stored in a platform-specific directory: - -| Platform | Location | -| -------- | ------------------------------------------------------------------ | -| Mac | `$HOME/Library/Application Support/rsconnect-python/` | -| Linux | `$HOME/.rsconnect-python/` or `$XDG_CONFIG_HOME/rsconnect-python/` | -| Windows | `$APPDATA/rsconnect-python` | - -Remembered server information is stored in the `servers.json` file in that directory. - -### Deployment Data - -After a deployment is completed, information about the deployment is saved -to enable later redeployment. This data is stored alongside the deployed file, -in an `rsconnect-python` subdirectory, if possible. If that location is not writable -during deployment, then the deployment data will be stored in the global configuration -directory specified above. - -
-Generated from rsconnect-python {{ rsconnect_python.version }} -
- -### Hide Jupyter Notebook Input Code Cells - -You can render a Jupyter notebook without its corresponding input code cells by passing the '--hide-all-input' flag through the cli: - -```bash -rsconnect deploy notebook \ - --server https://connect.example.org \ - --api-key my-api-key \ - --hide-all-input \ - my-notebook.ipynb -``` - -To selectively hide input cells in a Jupyter notebook, you need to do two things: - -1. tag cells with the 'hide_input' tag, -2. then pass the ' --hide-tagged-input' flag through the cli: - -```bash -rsconnect deploy notebook \ - --server https://connect.example.org \ - --api-key my-api-key \ - --hide-tagged-input \ - my-notebook.ipynb -``` - -By default, rsconnect-python does not install Jupyter notebook-related depenencies. -To use these hide input features in rsconnect-python you need to install these extra dependencies: - -``` -notebook -nbformat -nbconvert>=5.6.1 -``` - -## Content subcommands - -rsconnect-python supports multiple options for interacting with Posit Connect's -`/v1/content` API. Both administrators and publishers can use the content subcommands -to search, download, and rebuild content on Posit Connect without needing to access the -dashboard from a browser. - -> **Note** -> The `rsconnect content` CLI subcommands are intended to be easily scriptable. -> The default output format is `JSON` so that the results can be easily piped into -> other command line utilities like [`jq`](https://stedolan.github.io/jq/) for further post-processing. - -```bash -rsconnect content --help -# Usage: rsconnect content [OPTIONS] COMMAND [ARGS]... - -# Interact with Posit Connect's content API. - -# Options: -# --help Show this message and exit. - -# Commands: -# build Build content on Posit Connect. -# describe Describe a content item on Posit Connect. -# download-bundle Download a content item's source bundle. -# search Search for content on Posit Connect. -``` - -### Content Search - -The `rsconnect content search` subcommands can be used by administrators and publishers -to find specific content on a given Posit Connect server. The search returns -metadata for each content item that meets the search criteria. - -```bash -rsconnect content search --help -# Usage: rsconnect content search [OPTIONS] - -# Options: -# -n, --name TEXT The nickname of the Posit Connect server. -# -s, --server TEXT The URL for the Posit Connect server. -# -k, --api-key TEXT The API key to use to authenticate with -# Posit Connect. - -# -i, --insecure Disable TLS certification/host validation. -# -c, --cacert FILENAME The path to trusted TLS CA certificates. -# --published Search only published content. -# --unpublished Search only unpublished content. -# --content-type [unknown|shiny|rmd-static|rmd-shiny|static|api|tensorflow-saved-model|jupyter-static|python-api|python-dash|python-streamlit|python-bokeh|python-fastapi|python-gradio|quarto-shiny|quarto-static] -# Filter content results by content type. -# --r-version VERSIONSEARCHFILTER -# Filter content results by R version. -# --py-version VERSIONSEARCHFILTER -# Filter content results by Python version. -# --title-contains TEXT Filter content results by title. -# --order-by [created|last_deployed] -# Order content results. -# -v, --verbose Print detailed messages. -# --help Show this message and exit. - -rsconnect content search -# [ -# { -# "max_conns_per_process": null, -# "content_category": "", -# "load_factor": null, -# "cluster_name": "Local", -# "description": "", -# "bundle_id": "142", -# "image_name": null, -# "r_version": null, -# "content_url": "https://connect.example.org:3939/content/4ffc819c-065c-420c-88eb-332db1133317/", -# "connection_timeout": null, -# "min_processes": null, -# "last_deployed_time": "2021-12-02T18:09:11Z", -# "name": "logs-api-python", -# "title": "logs-api-python", -# "created_time": "2021-07-19T19:17:32Z", -# "read_timeout": null, -# "guid": "4ffc819c-065c-420c-88eb-332db1133317", -# "parameterized": false, -# "run_as": null, -# "py_version": "3.8.2", -# "idle_timeout": null, -# "app_role": "owner", -# "access_type": "acl", -# "app_mode": "python-api", -# "init_timeout": null, -# "id": "18", -# "quarto_version": null, -# "dashboard_url": "https://connect.example.org:3939/connect/#/apps/4ffc819c-065c-420c-88eb-332db1133317", -# "run_as_current_user": false, -# "owner_guid": "edf26318-0027-4d9d-bbbb-54703ebb1855", -# "max_processes": null -# }, -# ... -# ] -``` - -See [this section](#searching-for-content) for more comprehensive usage examples -of the available search flags. - - -### Content Build - -> **Note** -> The `rsconnect content build` subcommand requires Posit Connect >= 2021.11.1 - -Posit Connect caches R and Python packages in the configured -[`Server.DataDir`](https://docs.posit.co/connect/admin/appendix/configuration/#Server.DataDir). -Under certain circumstances (examples below), these package caches can become stale -and need to be rebuilt. This refresh automatically occurs when a Posit Connect -user visits the content. You may wish to refresh some content before it is visited -because it is high priority or is not visited frequently (API content, emailed reports). -In these cases, it is possible to preemptively build specific content items using -the `rsconnect content build` subcommands. This way the user does not have to pay -the build cost when the content is accessed next. - -The following are some common scenarios where performing a content build might be necessary: - -- OS upgrade -- changes to gcc or libc libraries -- changes to Python or R installations -- switching from source to binary package repositories or vice versa - -> **Note** -> The `content build` command is non-destructive, meaning that it does nothing to purge -> existing packrat/python package caches before a build. If you have an -> existing cache, it should be cleared prior to starting a content build. -> See the [migration documentation](https://docs.posit.co/connect/admin/appendix/cli/#migration) for details. - -> **Note** -> You may use the [`rsconnect content search`](#content-search) subcommand to help -> identify high priority content items to build. - -```bash -rsconnect content build --help -Usage: rsconnect content build [OPTIONS] COMMAND [ARGS]... - - Build content on Posit Connect. Requires Connect >= 2021.11.1 - -Options: - --help Show this message and exit. - -Commands: - add Mark a content item for build. Use `build run` to invoke the build - on the Connect server. - - history Get the build history for a content item. - logs Print the logs for a content build. - ls List the content items that are being tracked for build on a given - Connect server. - - rm Remove a content item from the list of content that are tracked for - build. Use `build ls` to view the tracked content. - - run Start building content on a given Connect server. -``` - -To build a specific content item, first `add` it to the list of content that is -"tracked" for building using its GUID. Content that is "tracked" in the local state -may become out-of-sync with what exists remotely on the Connect server (the result of -`rsconnect content search`). When this happens, it is safe to remove the locally tracked -entries with `rsconnect content build rm`. - -> **Note** -> Metadata for "tracked" content items is stored in a local directory called -> `rsconnect-build` which will be automatically created in your current working directory. -> You may set the environment variable `CONNECT_CONTENT_BUILD_DIR` to override this directory location. - -```bash -# `add` the content to mark it as "tracked" -rsconnect content build add --guid 4ffc819c-065c-420c-88eb-332db1133317 - -# run the build which kicks off a cache rebuild on the server -rsconnect content build run - -# once the build is complete, the content can be "untracked" -# this does not remove the content from the Connect server -# the entry is only removed from the local state file -rsconnect content build rm --guid 4ffc819c-065c-420c-88eb-332db1133317 -``` - -> **Note** -> See [this section](#add-to-build-from-search-results) for -> an example of how to add multiple content items in bulk, from the results -> of a `rsconnect content search` command. - -To view all currently "tracked" content items, use the `rsconnect content build ls` subcommand. - -```bash -rsconnect content build ls -``` - -To view only the "tracked" content items that have not yet been built, use the `--status NEEDS_BUILD` flag. - -```bash -rsconnect content build ls --status NEEDS_BUILD -``` - -Once the content items have been added, you may initiate a build -using the `rsconnect content build run` subcommand. This command will attempt to -build all "tracked" content that has the status `NEEDS_BUILD`. - -> To re-run failed builds, use `rsconnect content build run --retry`. This will build -all tracked content in any of the following states: `[NEEDS_BUILD, ABORTED, ERROR, RUNNING]`. -> -> If you encounter an error indicating that a build operation is already in progress, -you can use `rsconnect content build run --force` to bypass the check and proceed with building content marked as `NEEDS_BUILD`. -Ensure no other build operation is actively running before using the `--force` option. - -```bash -rsconnect content build run -# [INFO] 2021-12-14T13:02:45-0500 Initializing ContentBuildStore for https://connect.example.org:3939 -# [INFO] 2021-12-14T13:02:45-0500 Starting content build (https://connect.example.org:3939)... -# [INFO] 2021-12-14T13:02:45-0500 Starting build: 4ffc819c-065c-420c-88eb-332db1133317 -# [INFO] 2021-12-14T13:02:50-0500 Running = 1, Pending = 0, Success = 0, Error = 0 -# [INFO] 2021-12-14T13:02:50-0500 Build succeeded: 4ffc819c-065c-420c-88eb-332db1133317 -# [INFO] 2021-12-14T13:02:55-0500 Running = 0, Pending = 0, Success = 1, Error = 0 -# [INFO] 2021-12-14T13:02:55-0500 1/1 content builds completed in 0:00:10 -# [INFO] 2021-12-14T13:02:55-0500 Success = 1, Error = 0 -# [INFO] 2021-12-14T13:02:55-0500 Content build complete. -``` - -Sometimes content builds will fail and require debugging by the publisher or administrator. -Use the `rsconnect content build ls` to identify content builds that resulted in errors -and inspect the build logs with the `rsconnect content build logs` subcommand. - -```bash -rsconnect content build ls --status ERROR -# [INFO] 2021-12-14T13:07:32-0500 Initializing ContentBuildStore for https://connect.example.org:3939 -# [ -# { -# "rsconnect_build_status": "ERROR", -# "last_deployed_time": "2021-12-02T18:09:11Z", -# "owner_guid": "edf26318-0027-4d9d-bbbb-54703ebb1855", -# "rsconnect_last_build_log": "/Users/david/code/posit/rsconnect-python/rsconnect-build/logs/connect_example_org_3939/4ffc819c-065c-420c-88eb-332db1133317/pZoqfBoi6BgpKde5.log", -# "guid": "4ffc819c-065c-420c-88eb-332db1133317", -# "rsconnect_build_task_result": { -# "user_id": 1, -# "error": "Cannot find compatible environment: no compatible Local environment with Python version 3.9.5", -# "code": 1, -# "finished": true, -# "result": { -# "data": "An error occurred while building the content", -# "type": "build-failed-error" -# }, -# "id": "pZoqfBoi6BgpKde5" -# }, -# "dashboard_url": "https://connect.example.org:3939/connect/#/apps/4ffc819c-065c-420c-88eb-332db1133317", -# "name": "logs-api-python", -# "title": "logs-api-python", -# "content_url": "https://connect.example.org:3939/content/4ffc819c-065c-420c-88eb-332db1133317/", -# "bundle_id": "141", -# "rsconnect_last_build_time": "2021-12-14T18:07:16Z", -# "created_time": "2021-07-19T19:17:32Z", -# "app_mode": "python-api" -# } -# ] - -rsconnect content build logs --guid 4ffc819c-065c-420c-88eb-332db1133317 -# [INFO] 2021-12-14T13:09:27-0500 Initializing ContentBuildStore for https://connect.example.org:3939 -# Building Python API... -# Cannot find compatible environment: no compatible Local environment with Python version 3.9.5 -# Task failed. Task exited with status 1. -``` - -Once a build for a piece of tracked content is complete, it can be safely removed from the list of "tracked" -content by using `rsconnect content build rm` command. This command accepts a `--guid` argument to specify -which piece of content to remove. Removing the content from the list of tracked content simply removes the item -from the local state file, the content deployed to the server remains unchanged. - -```bash -rsconnect content build rm --guid 4ffc819c-065c-420c-88eb-332db1133317 -``` - -### Rebuilding lots of content - -When attempting to rebuild a long list of content, it is recommended to first build a sub-set of the content list. -First choose 1 or 2 Python and R content items for each version of Python and R on the server. Try to choose content -items that have the most dependencies in common with other content items on the server. Build these content items -first with the `rsconnect content build run` command. This will "warm" the Python and R environment cache for subsequent -content builds. Once these initial builds are complete, add the remaining content items to the list of "tracked" content -and execute another `rsconnect content build run` command. - -To execute multiple content builds simultaniously, use the `rsconnect content build run --parallelism` flag to increase the -number of concurrent builds. By default, each content item is built serially. Increasing the build parallelism can reduce the total -time needed to rebuild a long list of content items. We recommend starting with a low parallelism setting (2-3) and increasing -from there to avoid overloading the Connect server with concurrent build operations. Remember that these builds are executing on the -Connect server which consumes CPU, RAM, and i/o bandwidth that would otherwise we allocated for Python and R applications -running on the server. - -## Common Usage Examples - -### Searching for content - -The following are some examples of how publishers might use the -`rsconnect content search` subcommand to find content on Posit Connect. -By default, the `rsconnect content search` command will return metadata for ALL -of the content on a Posit Connect server, both published and unpublished content. - -> **Note** -> When using the `--r-version` and `--py-version` flags, users should -> make sure to quote the arguments to avoid conflicting with your shell. For -> example, bash would interpret `--py-version >3.0.0` as a shell redirect because of the -> unquoted `>` character. - -```bash -# return only published content -rsconnect content search --published - -# return only unpublished content -rsconnect content search --unpublished - -# return published content where the python version is at least 3.9.0 -rsconnect content search --published --py-version ">=3.9.0" - -# return published content where the R version is exactly 3.6.3 -rsconnect content search --published --r-version "==3.6.3" - -# return published content where the content type is a static RMD -rsconnect content search --content-type rmd-static - -# return published content where the content type is either shiny OR fast-api -rsconnect content search --content-type shiny --content-type python-fastapi - -# return all content, published or unpublished, where the title contains the -# text "Stock Report" -rsconnect content search --title-contains "Stock Report" - -# return published content, results are ordered by when the content was last -# deployed -rsconnect content search --published --order-by last_deployed - -# return published content, results are ordered by when the content was -# created -rsconnect content search --published --order-by created -``` - -### Finding r and python versions - -One common use for the `search` command might be to find the versions of -r and python that are currently in use on your Posit Connect server before a migration. - -```bash -# search for all published content and print the unique r and python version -# combinations -rsconnect content search --published | jq -c '.[] | {py_version,r_version}' | sort | -uniq -# {"py_version":"3.8.2","r_version":"3.5.3"} -# {"py_version":"3.8.2","r_version":"3.6.3"} -# {"py_version":"3.8.2","r_version":null} -# {"py_version":null,"r_version":"3.5.3"} -# {"py_version":null,"r_version":"3.6.3"} -# {"py_version":null,"r_version":null} -``` - -### Finding recently deployed content - -```bash -# return only the 10 most recently deployed content items -rsconnect content search \ - --order-by last_deployed \ - --published | jq -c 'limit(10; .[]) | { guid, last_deployed_time }' -# {"guid":"4ffc819c-065c-420c-88eb-332db1133317","last_deployed_time":"2021-12-02T18:09:11Z"} -# {"guid":"aa2603f8-1988-484f-a335-193f2c57e6c4","last_deployed_time":"2021-12-01T20:56:07Z"} -# {"guid":"051252f0-4f70-438f-9be1-d818a3b5f8d9","last_deployed_time":"2021-12-01T20:37:01Z"} -# {"guid":"015143da-b75f-407c-81b1-99c4a724341e","last_deployed_time":"2021-11-30T16:56:21Z"} -# {"guid":"bcc74209-3a81-4b9c-acd5-d24a597c256c","last_deployed_time":"2021-11-30T15:51:07Z"} -# {"guid":"f21d7767-c99e-4dd4-9b00-ff8ec9ae2f53","last_deployed_time":"2021-11-23T18:46:28Z"} -# {"guid":"da4f709c-c383-4fbc-89e2-f032b2d7e91d","last_deployed_time":"2021-11-23T18:46:28Z"} -# {"guid":"9180809d-38fd-4730-a0e0-8568c45d87b7","last_deployed_time":"2021-11-23T15:16:19Z"} -# {"guid":"2b1d2ab8-927d-4956-bbf9-29798d039bc5","last_deployed_time":"2021-11-22T18:33:17Z"} -# {"guid":"c96db3f3-87a1-4df5-9f58-eb109c397718","last_deployed_time":"2021-11-19T20:25:33Z"} -``` - -### Add to build from search results - -One common use case might be to `rsconnect content build add` content for build -based on the results of a `rsconnect content search`. For example: - -```bash -# search for all API type content, then -# for each guid, add it to the "tracked" content items -for guid in $(rsconnect content search \ - --published \ - --content-type python-api \ - --content-type api | jq -r '.[].guid'); do - rsconnect content build add --guid $guid -done -``` - -Adding content items one at a time can be a slow operation. This is because -`rsconnect content build add` must fetch metadata for each content item before it -is added to the "tracked" content items. By providing multiple `--guid` arguments -to the `rsconnect content build add` subcommand, we can fetch metadata for multiple content items -in a single api call, which speeds up the operation significantly. - -```bash -# write the guid of every published content item to a file called guids.txt -rsconnect content search --published | jq '.[].guid' > guids.txt - -# bulk-add from the guids.txt with a single `rsconnect content build add` command -xargs printf -- '-g %s\n' < guids.txt | xargs rsconnect content build add -``` -## Programmatic Provisioning - -Posit Connect supports the programmatic bootstrapping of an administrator API key -for scripted provisioning tasks. This process is supported by the `rsconnect bootstrap` command, -which uses a JSON Web Token to request an initial API key from a fresh Connect instance. - -```bash -rsconnect bootstrap \ - --server https://connect.example.org:3939 \ - --jwt-keypath /path/to/secret.key -``` - -A full description on how to use `rsconnect bootstrap` in a provisioning workflow is provided in the Connect administrator guide's -[programmatic provisioning](https://docs.posit.co/connect/admin/programmatic-provisioning) documentation. - -## Server Administration Tasks - -Starting with the 2023.05 edition of Posit Connect, `rsconnect-python` can be -used to perform certain server administration tasks, such as instance managing -runtime caches. For more information on runtime caches in Posit Connect, see the -Connect Admin Guide's section on [runtime -caches](https://docs.posit.co/connect/admin/server-management/runtime-caches/). - -Examples in this section will use `--name myserver` to stand in for your Connect -server information. See [Managing Server -Information](#managing-server-information) above for more details. - -### Enumerate Runtime Caches -*New in Connect 2023.05* - -Use the command below to enumerate runtime caches on a Connect server. The -command will output a JSON object containing a list of runtime caches . Each -cache entry will contain the following information: - -- `language`: The language of content that uses the cache, either R or Python. -- `version`: The language version of the content that uses the cache. -- `image_name`: The execution environment of the cache. The string `Local` - denotes native execution. For Connect instances that use off-host execution, - the name of the image that uses the cache will be displayed. - -```bash -rsconnect system caches list --name myserver -# { -# "caches": [ -# { -# "language": "R", -# "version": "3.6.3", -# "image_name": "Local" -# }, -# { -# "language": "Python", -# "version": "3.9.5", -# "image_name": "Local" -# }, -# { -# "language": "R", -# "version": "3.6.3", -# "image_name": "rstudio/content-base:r3.6.3-py3.9.5-bionic" -# }, -# { -# "language": "Python", -# "version": "3.9.5", -# "image_name": "rstudio/content-base:r3.6.3-py3.9.5-bionic" -# } -# ] -# } -``` - -> **Note** -> The `image_name` field returned by the server will use sanitized versions -> of names. - -### Delete Runtime Caches -*New in Connect 2023.05* - -When Connect's execution environment changes, runtime caches may be invalidated. -In these cases, you will need to delete the affected runtime caches using the -`system caches delete` command. - -> **Warning** -> After deleting a cache, the first time affected content is visited, Connect -> will need to reconstruct its environment. This can take a long time. To -> mitigate this, you can use the [`content build`](#content-build) command to -> rebuild affected content ahead of time. You may want to do this just for -> high-priority content, or for all content. - -To delete a runtime cache, call the `system caches delete` command, specifying a -Connect server, as well as the language (`-l, --language`), version (`-V, ---version`), and image name (`-I, --image-name`) for the cache you wish to -delete. Deleting a large cache might take a while. The command will wait for -Connect to finish the task. - -Use the following parameters specify the target cache: - -- `language` (required) must name `R` or `Python`. It is case-insensitive. -- `version` (required) must be a three-part version number, e.g. `3.8.12`. -- `image-name` (optional) defaults to `Local`, which targets caches used for - natively-executed content. Off-host images can be specified using either the - literal image name or the sanitized name returned by the `list` command. - -Use the dry run flag (`-d, --dry-run`) to surface any errors ahead of -deletion. - -```bash -rsconnect system caches delete \ - --name myserver \ - --language Python \ - --version 3.9.5 \ - --image-name rstudio/content-base:r3.6.3-py3.9.5-bionic \ - --dry-run -# Dry run finished - -rsconnect system caches delete \ - --name myserver \ - --language Python \ - --version 3.9.5 \ - --image-name rstudio/content-base:r3.6.3-py3.9.5-bionic -# Deleting runtime cache... -# Successfully deleted runtime cache -``` +## Contributing -You should run these commands for each cache you wish to delete. +[Contributing docs](./CONTRIBUTING.md) diff --git a/CHANGELOG.md b/docs/CHANGELOG.md similarity index 100% rename from CHANGELOG.md rename to docs/CHANGELOG.md diff --git a/docs/README.md b/docs/README.md deleted file mode 100644 index 67718e8a..00000000 --- a/docs/README.md +++ /dev/null @@ -1,30 +0,0 @@ -# Documentation - -The top-level [README.md](../README.md) becomes our documentation. GitHub -supports a very small set of admonitions. Those admonitions are rewritten into -mkdocs-style admonitions when the README is rendered for our hosted -documentation. - -Write GitHub-style admonitions, which MUST have the header as a separate line -using the following syntax; the entire Markdown blockquote becomes the mkdocs -admonition. - -GitHub README input: - -```markdown -> **Warning** -> This is the warning text. - -> **Note** -> This is the note text. -``` - -mkdocs output: - -```markdown -!!! warning - This is the warning text. - -!!! note - This is the note text. -``` diff --git a/docs/commands/add.md b/docs/commands/add.md new file mode 100644 index 00000000..0cb4ef98 --- /dev/null +++ b/docs/commands/add.md @@ -0,0 +1,3 @@ +::: mkdocs-click + :module: rsconnect.main + :command: add diff --git a/docs/commands/bootstrap.md b/docs/commands/bootstrap.md new file mode 100644 index 00000000..49a5ecf1 --- /dev/null +++ b/docs/commands/bootstrap.md @@ -0,0 +1,3 @@ +::: mkdocs-click + :module: rsconnect.main + :command: bootstrap diff --git a/docs/commands/content.md b/docs/commands/content.md new file mode 100644 index 00000000..d281ca11 --- /dev/null +++ b/docs/commands/content.md @@ -0,0 +1,3 @@ +::: mkdocs-click + :module: rsconnect.main + :command: content diff --git a/docs/commands/deploy.md b/docs/commands/deploy.md new file mode 100644 index 00000000..9d5b05e5 --- /dev/null +++ b/docs/commands/deploy.md @@ -0,0 +1,3 @@ +::: mkdocs-click + :module: rsconnect.main + :command: deploy diff --git a/docs/commands/details.md b/docs/commands/details.md new file mode 100644 index 00000000..737d8ba2 --- /dev/null +++ b/docs/commands/details.md @@ -0,0 +1,3 @@ +::: mkdocs-click + :module: rsconnect.main + :command: details diff --git a/docs/commands/info.md b/docs/commands/info.md new file mode 100644 index 00000000..12208f0c --- /dev/null +++ b/docs/commands/info.md @@ -0,0 +1,3 @@ +::: mkdocs-click + :module: rsconnect.main + :command: info diff --git a/docs/commands/list.md b/docs/commands/list.md new file mode 100644 index 00000000..fe6a4ff2 --- /dev/null +++ b/docs/commands/list.md @@ -0,0 +1,3 @@ +::: mkdocs-click + :module: rsconnect.main + :command: list_servers diff --git a/docs/commands/remove.md b/docs/commands/remove.md new file mode 100644 index 00000000..06b5a234 --- /dev/null +++ b/docs/commands/remove.md @@ -0,0 +1,3 @@ +::: mkdocs-click + :module: rsconnect.main + :command: remove diff --git a/docs/commands/system.md b/docs/commands/system.md new file mode 100644 index 00000000..01630f92 --- /dev/null +++ b/docs/commands/system.md @@ -0,0 +1,3 @@ +::: mkdocs-click + :module: rsconnect.main + :command: system diff --git a/docs/commands/version.md b/docs/commands/version.md new file mode 100644 index 00000000..f0c545ab --- /dev/null +++ b/docs/commands/version.md @@ -0,0 +1,3 @@ +::: mkdocs-click + :module: rsconnect.main + :command: version diff --git a/docs/commands/write-manifest.md b/docs/commands/write-manifest.md new file mode 100644 index 00000000..7b87cca6 --- /dev/null +++ b/docs/commands/write-manifest.md @@ -0,0 +1,3 @@ +::: mkdocs-click + :module: rsconnect.main + :command: write_manifest diff --git a/docs/docs/css/custom.css b/docs/css/custom.css similarity index 100% rename from docs/docs/css/custom.css rename to docs/css/custom.css diff --git a/docs/docs/images/favicon.ico b/docs/images/favicon.ico similarity index 100% rename from docs/docs/images/favicon.ico rename to docs/images/favicon.ico diff --git a/docs/docs/images/iconPositConnect.svg b/docs/images/iconPositConnect.svg similarity index 100% rename from docs/docs/images/iconPositConnect.svg rename to docs/images/iconPositConnect.svg diff --git a/docs/docs/images/posit-logo-fullcolor-TM.svg b/docs/images/posit-logo-fullcolor-TM.svg similarity index 100% rename from docs/docs/images/posit-logo-fullcolor-TM.svg rename to docs/images/posit-logo-fullcolor-TM.svg diff --git a/docs/docs/images/positLogoBlack.svg b/docs/images/positLogoBlack.svg similarity index 100% rename from docs/docs/images/positLogoBlack.svg rename to docs/images/positLogoBlack.svg diff --git a/docs/docs/images/positLogoWhite.svg b/docs/images/positLogoWhite.svg similarity index 100% rename from docs/docs/images/positLogoWhite.svg rename to docs/images/positLogoWhite.svg diff --git a/docs/docs/images/rstudio-logo.png b/docs/images/rstudio-logo.png similarity index 100% rename from docs/docs/images/rstudio-logo.png rename to docs/images/rstudio-logo.png diff --git a/docs/index.md b/docs/index.md new file mode 100644 index 00000000..24dc83c2 --- /dev/null +++ b/docs/index.md @@ -0,0 +1,1218 @@ +# The rsconnect-python CLI + +This package provides a CLI (command-line interface) for interacting +with and deploying to Posit Connect. Many types of content supported by Posit +Connect may be deployed by this package, including WSGI-style APIs, Dash, Streamlit, +Gradio, and Bokeh applications. + +Content types not directly supported by the CLI may also be deployed if they include a +prepared `manifest.json` file. See ["Deploying R or Other +Content"](#deploying-r-or-other-content) for details. + + +### Installation + +To install `rsconnect-python` from PYPI, you may use any python package manager such as +pip: + +```bash +pip install rsconnect-python +``` + +You may also build and install a wheel directly from a repository clone: + +```bash +git clone https://github.com/posit-dev/rsconnect-python.git +cd rsconnect-python +pip install pipenv +make dist +pip install ./dist/rsconnect_python-*.whl +``` + +### Using the rsconnect CLI + +Here's an example command that deploys a Jupyter notebook to Posit Connect. + +```bash +rsconnect deploy notebook \ + --server https://connect.example.org \ + --api-key my-api-key \ + my-notebook.ipynb +``` + +> **Note** +> The examples here use long command line options, but there are short +> options (`-s`, `-k`, etc.) available also. Run `rsconnect deploy notebook --help` +> for details. + +### Setting up `rsconnect` CLI auto-completion + +If you would like to use your shell's tab completion support with the `rsconnect` +command, use the command below for the shell you are using. + +#### `bash` + +If you are using the `bash` shell, use this to enable tab completion. + +```bash +#~/.bashrc +eval "$(_RSCONNECT_COMPLETE=source rsconnect)" +``` + +#### `zsh` + +If you are using the `zsh` shell, use this to enable tab completion. + +```zsh +#~/.zshrc +eval "$(_RSCONNECT_COMPLETE=source_zsh rsconnect)" +``` + +If you get `command not found: compdef`, you need to add the following lines to your +`.zshrc` before the completion setup: + +```zsh +#~/.zshrc +autoload -Uz compinit +compinit +``` + +### Managing Server Information + +The information used by the `rsconnect` command to communicate with a Posit Connect +server can be tedious to repeat on every command. To help, the CLI supports the idea +of saving this information, making it usable by a simple nickname. + +> **Warning** +> One item of information saved is the API key used to authenticate with +> Posit Connect. Although the file where this information is saved is marked as +> accessible by the owner only, it's important to remember that the key is present +> in the file as plain text so care must be taken to prevent any unauthorized access +> to the server information file. + +#### TLS Support and Posit Connect + +Usually, a Posit Connect server will be set up to be accessed in a secure manner, +using the `https` protocol rather than simple `http`. If Posit Connect is set up +with a self-signed certificate, you will need to include the `--insecure` flag on +all commands. If Posit Connect is set up to require a client-side certificate chain, +you will need to include the `--cacert` option that points to your certificate +authority (CA) trusted certificates file. Both of these options can be saved along +with the URL and API Key for a server. + +> **Note** +> When certificate information is saved for the server, the specified file +> is read and its _contents_ are saved under the server's nickname. If the CA file's +> contents are ever changed, you will need to add the server information again. + +See the [Network Options](#network-options) section for more details about these options. + +#### Remembering Server Information + +Use the `add` command to store information about a Posit Connect server: + +```bash +rsconnect add \ + --api-key my-api-key \ + --server https://connect.example.org \ + --name myserver +``` + +> **Note** +> The `rsconnect` CLI will verify that the serve URL and API key +> are valid. If either is found not to be, no information will be saved. + +If any of the access information for the server changes, simply rerun the +`add` command with the new information and it will replace the original +information. + +Once the server's information is saved, you can refer to it by its nickname: + +```bash +rsconnect deploy notebook --name myserver my-notebook.ipynb +``` + +If there is information for only one server saved, this will work too: + +```bash +rsconnect deploy notebook my-notebook.ipynb +``` + +#### Listing Server Information + +You can see the list of saved server information with: + +``` +rsconnect list +``` + +#### Removing Server Information + +You can remove information about a server with: + +``` +rsconnect remove --name myserver +``` + +Removing may be done by its nickname (`--name`) or URL (`--server`). + +### Verifying Server Information + +You can verify that a URL refers to a running instance of Posit Connect by using +the `details` command: + +```bash +rsconnect details --server https://connect.example.org +``` + +In this form, `rsconnect` will only tell you whether the URL given does, in fact, refer +to a running Posit Connect instance. If you include a valid API key: + +```bash +rsconnect details --server https://connect.example.org --api-key my-api-key +``` + +the tool will provide the version of Posit Connect (if the server is configured to +divulge that information) and environmental information including versions of Python +that are installed on the server. + +You can also use nicknames with the `details` command if you want to verify that the +stored information is still valid. + +### Notebook Deployment Options + +There are a variety of options available to you when deploying a Jupyter notebook to +Posit Connect. + +#### Including Extra Files + +You can include extra files in the deployment bundle to make them available when your +notebook is run by the Posit Connect server. Just specify them on the command line +after the notebook file: + +```bash +rsconnect deploy notebook my-notebook.ipynb data.csv +``` + +#### Package Dependencies + +If a `requirements.txt` file exists in the same directory as the notebook file, it will +be included in the bundle. It must specify the package dependencies needed to execute +the notebook. Posit Connect will reconstruct the Python environment using the +specified package list. + +If there is no `requirements.txt` file or the `--force-generate` option is specified, +the package dependencies will be determined from the current Python environment, or +from an alternative Python executable specified via the `--python` option: + +```bash +rsconnect deploy notebook --python /path/to/python my-notebook.ipynb +``` + +You can see the packages list that will be included by running `pip list --format=freeze` yourself, +ensuring that you use the same Python that you use to run your Jupyter Notebook: + +```bash +/path/to/python -m pip list --format=freeze +``` + +#### Python Version + +When deploying Python content to Posit Connect, +the server will require a version of Python that matches the content +requirements. + +For example, a server with only Python 3.9 installed will fail to match content +that requires Python 3.8. + +`rsconnect` supports detecting Python version requirements in several ways: + 1. A `.python-version` file exists. In such case + `rsconnect` will use its content to determine the python version requirement. + 2. A `pyproject.toml` with a `project.requires-python` field exists. + In such case the requirement specified in the field will be used + if no `.python-version` file exists. + 3. A `setup.cfg` with an `options.python_requires` field exists. + In such case the requirement specified in the field will be used + if **1** or **2** were not already satisfied. + 4. If no other source of version requirement was found, then + the interpreter in use is considered the one required to run the content. + +On Posit Connect `>=2025.03.0` the requirement detected by `rsconnect` is +always respected. Older Connect versions will instead rely only on the +python version used to deploy the content to determine the requirement. + +For more information see the [Posit Connect Admin Guide chapter titled Python Version +Matching](https://docs.posit.co/connect/admin/python/#python-version-matching). + +We recommend providing a `pyproject.toml` with a `project.requires-python` field +if the deployed content is an installable package and a `.python-version` file +for plain directories. + +> **Note** +> The packages and package versions listed in `requirements.txt` must be +> compatible with the Python version you request. + + +#### Static (Snapshot) Deployment + +By default, `rsconnect` deploys the original notebook with all its source code. This +enables the Posit Connect server to re-run the notebook upon request or on a schedule. + +If you just want to publish an HTML snapshot of the notebook, you can use the `--static` +option. This will cause `rsconnect` to execute your notebook locally to produce the HTML +file, then publish the HTML file to the Posit Connect server: + +```bash +rsconnect deploy notebook --static my-notebook.ipynb +``` + +### Creating a Manifest for Future Deployment + +You can create a `manifest.json` file for a Jupyter Notebook, then use that manifest +in a later deployment. Use the `write-manifest` command to do this. + +The `write-manifest` command will also create a `requirements.txt` file, if it does +not already exist or the `--force-generate` option is specified. It will contain the +package dependencies from the current Python environment, or from an alternative +Python executable specified in the `--python` option. + +Here is an example of the `write-manifest` command: + +```bash +rsconnect write-manifest notebook my-notebook.ipynb +``` + +> **Note** +> Manifests for static (pre-rendered) notebooks cannot be created. + +### API/Application Deployment Options + +You can deploy a variety of APIs and applications using sub-commands of the +`rsconnect deploy` command. + +* `api`: WSGI-compliant APIs (e.g., `bottle`, `falcon`, `flask`, `flask-restx`, `flasgger`, `pycnic`). +* `flask`: Flask APIs (_Note: `flask` is an alias of `api`._). +* `fastapi`: ASGI-compliant APIs (e.g, `fastapi`, `quart`, `sanic`, `starlette`) +* `dash`: Python Dash apps +* `streamlit`: Streamlit apps +* `bokeh`: Bokeh server apps +* `gradio`: Gradio apps + +All options below apply equally to the `api`, `fastapi`, `dash`, `streamlit`, +`gradio`, and `bokeh` sub-commands. + +#### Including Extra Files + +You can include extra files in the deployment bundle to make them available when your +API or application is run by the Posit Connect server. Just specify them on the +command line after the API or application directory: + +```bash +rsconnect deploy api flask-api/ data.csv +``` + +Since deploying an API or application starts at a directory level, there will be times +when some files under that directory subtree should not be included in the deployment +or manifest. Use the `--exclude` option to specify files or directories to exclude. + +```bash +rsconnect deploy dash --exclude dash-app-venv --exclude TODO.txt dash-app/ +``` + +You can exclude a directory by naming it: +```bash +rsconnect deploy dash --exclude dash-app-venv --exclude output/ dash-app/ +``` + +The `--exclude` option may be repeated, and may include a glob pattern. +You should always quote a glob pattern so that it will be passed to `rsconnect` as-is +instead of letting the shell expand it. If a file is specifically listed as an extra +file that also matches an exclusion pattern, the file will still be included in the +deployment (i.e., extra files take precedence). + +```bash +rsconnect deploy dash --exclude dash-app-venv --exclude “*.txt” dash-app/ +``` + +The following shows an example of an extra file taking precedence: + +```bash +rsconnect deploy dash --exclude “*.csv” dash-app/ important_data.csv +``` + +The "`**`" glob pattern will recursively match all files and directories, +while "`*`" only matches files. The "`**`" pattern is useful with complicated +project hierarchies where enumerating the _included_ files is simpler than +listing the _exclusions_. + +```bash +rsconnect deploy quarto . _quarto.yml index.qmd requirements.txt --exclude "**" +``` + +Some directories are excluded by default, to prevent bundling and uploading files that are not needed or might interfere with the deployment process: + +``` +.Rproj.user +.env +.git +.svn +.venv +__pycache__ +env +packrat +renv +rsconnect-python +rsconnect +venv +``` + +Any directory that appears to be a Python virtual environment (by containing +`bin/python`) will also be excluded. + + +#### Package Dependencies + +If a `requirements.txt` file exists in the API/application directory, it will be +included in the bundle. It must specify the package dependencies needed to execute +the API or application. Posit Connect will reconstruct the Python environment using +the specified package list. + +If there is no `requirements.txt` file or the `--force-generate` option is specified, +the package dependencies will be determined from the current Python environment, or +from an alternative Python executable specified via the `--python` option: + +```bash +rsconnect deploy api --python /path/to/python my-api/ +``` + +You can see the packages list that will be included by running `pip list --format=freeze` yourself, +ensuring that you use the same Python that you use to run your API or application: + +```bash +/path/to/python -m pip list --format=freeze +``` + +#### Python Version + +When deploying Python content to Posit Connect, +the server will require matching `` versions of Python. For example, +a server with only Python 3.9 installed will fail to match content deployed with +Python 3.8. Your administrator may also enable exact Python version matching which +will be stricter and require matching major, minor, and patch versions. For more +information see the [Posit Connect Admin Guide chapter titled Python Version +Matching](https://docs.posit.co/connect/admin/python/#python-version-matching). + +We recommend installing a version of Python on your client that is also available +in your Connect installation. If that's not possible, you can override +rsconnect-python's detected Python version and request a version of Python +that is installed in Connect, For example, this command: + +```bash +rsconnect deploy api --override-python-version 3.11.5 my-api/ +``` + +will deploy the content in `my-api` while requesting that Connect +use Python version 3.11.5. + +> **Note** +> The packages and package versions listed in `requirements.txt` must be +> compatible with the Python version you request. + +### Creating a Manifest for Future Deployment + +You can create a `manifest.json` file for an API or application, then use that +manifest in a later deployment. Use the `write-manifest` command to do this. + +The `write-manifest` command will also create a `requirements.txt` file, if it does +not already exist or the `--force-generate` option is specified. It will contain +the package dependencies from the current Python environment, or from an alternative +Python executable specified in the `--python` option. + +Here is an example of the `write-manifest` command: + +```bash +rsconnect write-manifest api my-api/ +``` + +### Deploying R or Other Content + +You can deploy other content that has an existing Posit Connect `manifest.json` +file. For example, if you download and unpack a source bundle from Posit Connect, +you can deploy the resulting directory. The options are similar to notebook or +API/application deployment; see `rsconnect deploy manifest --help` for details. + +Here is an example of the `deploy manifest` command: + +```bash +rsconnect deploy manifest /path/to/manifest.json +``` + +> **Note** +> In this case, the existing content is deployed as-is. Python environment +> inspection and notebook pre-rendering, if needed, are assumed to be done already +> and represented in the manifest. + +The argument to `deploy manifest` may also be a directory so long as that directory +contains a `manifest.json` file. + +If you have R content but don't have a `manifest.json` file, you can use the RStudio +IDE to create the manifest. See the help for the `rsconnect::writeManifest` R function: + +```r +install.packages('rsconnect') +library(rsconnect) +?rsconnect::writeManifest +``` + +### Options for All Types of Deployments + +These options apply to any type of content deployment. + +#### Title + +The title of the deployed content is, by default, derived from the filename. For +example, if you deploy `my-notebook.ipynb`, the title will be `my-notebook`. To change +this, use the `--title` option: + +``` +rsconnect deploy notebook --title "My Notebook" my-notebook.ipynb +``` + +When using `rsconnect deploy api`, `rsconnect deploy fastapi`, `rsconnect deploy dash`, +`rsconnect deploy streamlit`, `rsconnect deploy bokeh`, or `rsconnect deploy gradio`, +the title is derived from the directory containing the API or application. + +When using `rsconnect deploy manifest`, the title is derived from the primary +filename referenced in the manifest. + +#### Verification After Deployment +After deploying your content, rsconnect accesses the deployed content +to verify that the deployment is live. This is done with a `GET` request +to the content, without parameters. The request is +considered successful if there isn't a 5xx code returned. Errors like +400 Bad Request or 405 Method Not Allowed because not all apps support `GET /`. +For cases where this is not desired, use the `--no-verify` flag on the command line. + +### Environment variables +You can set environment variables during deployment. Their names and values will be +passed to Posit Connect during deployment so you can use them in your code. Note that +if you are using `rsconnect` to deploy to shinyapps.io, environment variable management +is not supported on that platform. + +For example, if `notebook.ipynb` contains +```python +print(os.environ["MYVAR"]) +``` + +You can set the value of `MYVAR` that will be set when your code runs in Posit Connect +using the `-E/--environment` option: +```bash +rsconnect deploy notebook --environment MYVAR='hello world' notebook.ipynb +``` + +To avoid exposing sensitive values on the command line, you can specify +a variable without a value. In this case, it will use the value from the +environment in which rsconnect-python is running: +```bash +export SECRET_KEY=12345 + +rsconnect deploy notebook --environment SECRET_KEY notebook.ipynb +``` + +If you specify environment variables when updating an existing deployment, +new values will be set for the variables you provided. Other variables will +remain unchanged. If you don't specify any variables, all of the existing +variables will remain unchanged. + +Environment variables are set on the content item before the content bundle +is uploaded and deployed. If the deployment fails, the new environment variables +will still take effect. + +### Network Options + +When specifying information that `rsconnect` needs to be able to interact with Posit +Connect, you can tailor how transport layer security is performed. + +#### TLS/SSL Certificates + +Posit Connect servers can be configured to use TLS/SSL. If your server's certificate +is trusted by your Jupyter Notebook server, API client or user's browser, then you +don't need to do anything special. You can test this out with the `details` command: + +```bash +rsconnect details \ + --api-key my-api-key \ + --server https://connect.example.org:3939 +``` + +If this fails with a TLS Certificate Validation error, then you have two options. + +* Provide the Root CA certificate that is at the root of the signing chain for your + Posit Connect server. This will enable `rsconnect` to securely validate the + server's TLS certificate. + + ```bash + rsconnect details \ + --api-key my-api-key \ + --server https://connect.example.org \ + --cacert /path/to/certificate.pem + ``` + +* Posit Connect is in "insecure mode". This disables TLS certificate verification, + which results in a less secure connection. + + ```bash + rsconnect add \ + --api-key my-api-key \ + --server https://connect.example.org \ + --insecure + ``` + +Once you work out the combination of options that allow you to successfully work with +an instance of Posit Connect, you'll probably want to use the `add` command to have +`rsconnect` remember those options and allow you to just use a nickname. + +### Updating a Deployment + +If you deploy a file again to the same server, `rsconnect` will update the previous +deployment. This means that you can keep running `rsconnect deploy notebook my-notebook.ipynb` +as you develop new versions of your notebook. The same applies to other Python content +types. + +#### Forcing a New Deployment + +To bypass this behavior and force a new deployment, use the `--new` option: + +```bash +rsconnect deploy dash --new my-app/ +``` + +#### Updating a Different Deployment + +If you want to update an existing deployment but don't have the saved deployment data, +you can provide the app's numeric ID or GUID on the command line: + +```bash +rsconnect deploy notebook --app-id 123456 my-notebook.ipynb +``` + +You must be the owner of the target deployment, or a collaborator with permission to +change the content. The type of content (static notebook, notebook with source code, +API, or application) must match the existing deployment. + +> **Note** +> There is no confirmation required to update a deployment. If you do so +> accidentally, use the "Source Versions" dialog in the Posit Connect dashboard to +> activate the previous version and remove the erroneous one. + +##### Finding the App ID + +The App ID associated with a piece of content you have previously deployed from the +`rsconnect` command line interface can be found easily by querying the deployment +information using the `info` command. For more information, see the +[Showing the Deployment Information](#showing-the-deployment-information) section. + +If the content was deployed elsewhere or `info` does not return the correct App ID, +but you can open the content on Posit Connect, find the content and open it in a +browser. The URL in your browser's location bar will contain `#/apps/NNN` where `NNN` +is your App ID. The GUID identifier for the app may be found on the **Info** tab for +the content in the Posit Connect UI. + +#### Showing the Deployment Information + +You can see the information that the `rsconnect` command has saved for the most recent +deployment with the `info` command: + +```bash +rsconnect info my-notebook.ipynb +``` + +If you have deployed to multiple servers, the most recent deployment information for +each server will be shown. This command also displays the path to the file where the +deployment data is stored. + +## Stored Information Files + +Stored information files are stored in a platform-specific directory: + +| Platform | Location | +| -------- | ------------------------------------------------------------------ | +| Mac | `$HOME/Library/Application Support/rsconnect-python/` | +| Linux | `$HOME/.rsconnect-python/` or `$XDG_CONFIG_HOME/rsconnect-python/` | +| Windows | `$APPDATA/rsconnect-python` | + +Remembered server information is stored in the `servers.json` file in that directory. + +### Deployment Data + +After a deployment is completed, information about the deployment is saved +to enable later redeployment. This data is stored alongside the deployed file, +in an `rsconnect-python` subdirectory, if possible. If that location is not writable +during deployment, then the deployment data will be stored in the global configuration +directory specified above. + +
+Generated from rsconnect-python {{ rsconnect_python.version }} +
+ +### Hide Jupyter Notebook Input Code Cells + +You can render a Jupyter notebook without its corresponding input code cells by passing the '--hide-all-input' flag through the cli: + +```bash +rsconnect deploy notebook \ + --server https://connect.example.org \ + --api-key my-api-key \ + --hide-all-input \ + my-notebook.ipynb +``` + +To selectively hide input cells in a Jupyter notebook, you need to do two things: + +1. tag cells with the 'hide_input' tag, +2. then pass the ' --hide-tagged-input' flag through the cli: + +```bash +rsconnect deploy notebook \ + --server https://connect.example.org \ + --api-key my-api-key \ + --hide-tagged-input \ + my-notebook.ipynb +``` + +By default, rsconnect-python does not install Jupyter notebook-related depenencies. +To use these hide input features in rsconnect-python you need to install these extra dependencies: + +``` +notebook +nbformat +nbconvert>=5.6.1 +``` + +## Content subcommands + +rsconnect-python supports multiple options for interacting with Posit Connect's +`/v1/content` API. Both administrators and publishers can use the content subcommands +to search, download, and rebuild content on Posit Connect without needing to access the +dashboard from a browser. + +> **Note** +> The `rsconnect content` CLI subcommands are intended to be easily scriptable. +> The default output format is `JSON` so that the results can be easily piped into +> other command line utilities like [`jq`](https://stedolan.github.io/jq/) for further post-processing. + +```bash +rsconnect content --help +# Usage: rsconnect content [OPTIONS] COMMAND [ARGS]... + +# Interact with Posit Connect's content API. + +# Options: +# --help Show this message and exit. + +# Commands: +# build Build content on Posit Connect. +# describe Describe a content item on Posit Connect. +# download-bundle Download a content item's source bundle. +# search Search for content on Posit Connect. +``` + +### Content Search + +The `rsconnect content search` subcommands can be used by administrators and publishers +to find specific content on a given Posit Connect server. The search returns +metadata for each content item that meets the search criteria. + +```bash +rsconnect content search --help +# Usage: rsconnect content search [OPTIONS] + +# Options: +# -n, --name TEXT The nickname of the Posit Connect server. +# -s, --server TEXT The URL for the Posit Connect server. +# -k, --api-key TEXT The API key to use to authenticate with +# Posit Connect. + +# -i, --insecure Disable TLS certification/host validation. +# -c, --cacert FILENAME The path to trusted TLS CA certificates. +# --published Search only published content. +# --unpublished Search only unpublished content. +# --content-type [unknown|shiny|rmd-static|rmd-shiny|static|api|tensorflow-saved-model|jupyter-static|python-api|python-dash|python-streamlit|python-bokeh|python-fastapi|python-gradio|quarto-shiny|quarto-static] +# Filter content results by content type. +# --r-version VERSIONSEARCHFILTER +# Filter content results by R version. +# --py-version VERSIONSEARCHFILTER +# Filter content results by Python version. +# --title-contains TEXT Filter content results by title. +# --order-by [created|last_deployed] +# Order content results. +# -v, --verbose Print detailed messages. +# --help Show this message and exit. + +rsconnect content search +# [ +# { +# "max_conns_per_process": null, +# "content_category": "", +# "load_factor": null, +# "cluster_name": "Local", +# "description": "", +# "bundle_id": "142", +# "image_name": null, +# "r_version": null, +# "content_url": "https://connect.example.org:3939/content/4ffc819c-065c-420c-88eb-332db1133317/", +# "connection_timeout": null, +# "min_processes": null, +# "last_deployed_time": "2021-12-02T18:09:11Z", +# "name": "logs-api-python", +# "title": "logs-api-python", +# "created_time": "2021-07-19T19:17:32Z", +# "read_timeout": null, +# "guid": "4ffc819c-065c-420c-88eb-332db1133317", +# "parameterized": false, +# "run_as": null, +# "py_version": "3.8.2", +# "idle_timeout": null, +# "app_role": "owner", +# "access_type": "acl", +# "app_mode": "python-api", +# "init_timeout": null, +# "id": "18", +# "quarto_version": null, +# "dashboard_url": "https://connect.example.org:3939/connect/#/apps/4ffc819c-065c-420c-88eb-332db1133317", +# "run_as_current_user": false, +# "owner_guid": "edf26318-0027-4d9d-bbbb-54703ebb1855", +# "max_processes": null +# }, +# ... +# ] +``` + +See [this section](#searching-for-content) for more comprehensive usage examples +of the available search flags. + + +### Content Build + +> **Note** +> The `rsconnect content build` subcommand requires Posit Connect >= 2021.11.1 + +Posit Connect caches R and Python packages in the configured +[`Server.DataDir`](https://docs.posit.co/connect/admin/appendix/configuration/#Server.DataDir). +Under certain circumstances (examples below), these package caches can become stale +and need to be rebuilt. This refresh automatically occurs when a Posit Connect +user visits the content. You may wish to refresh some content before it is visited +because it is high priority or is not visited frequently (API content, emailed reports). +In these cases, it is possible to preemptively build specific content items using +the `rsconnect content build` subcommands. This way the user does not have to pay +the build cost when the content is accessed next. + +The following are some common scenarios where performing a content build might be necessary: + +- OS upgrade +- changes to gcc or libc libraries +- changes to Python or R installations +- switching from source to binary package repositories or vice versa + +> **Note** +> The `content build` command is non-destructive, meaning that it does nothing to purge +> existing packrat/python package caches before a build. If you have an +> existing cache, it should be cleared prior to starting a content build. +> See the [migration documentation](https://docs.posit.co/connect/admin/appendix/cli/#migration) for details. + +> **Note** +> You may use the [`rsconnect content search`](#content-search) subcommand to help +> identify high priority content items to build. + +```bash +rsconnect content build --help +Usage: rsconnect content build [OPTIONS] COMMAND [ARGS]... + + Build content on Posit Connect. Requires Connect >= 2021.11.1 + +Options: + --help Show this message and exit. + +Commands: + add Mark a content item for build. Use `build run` to invoke the build + on the Connect server. + + history Get the build history for a content item. + logs Print the logs for a content build. + ls List the content items that are being tracked for build on a given + Connect server. + + rm Remove a content item from the list of content that are tracked for + build. Use `build ls` to view the tracked content. + + run Start building content on a given Connect server. +``` + +To build a specific content item, first `add` it to the list of content that is +"tracked" for building using its GUID. Content that is "tracked" in the local state +may become out-of-sync with what exists remotely on the Connect server (the result of +`rsconnect content search`). When this happens, it is safe to remove the locally tracked +entries with `rsconnect content build rm`. + +> **Note** +> Metadata for "tracked" content items is stored in a local directory called +> `rsconnect-build` which will be automatically created in your current working directory. +> You may set the environment variable `CONNECT_CONTENT_BUILD_DIR` to override this directory location. + +```bash +# `add` the content to mark it as "tracked" +rsconnect content build add --guid 4ffc819c-065c-420c-88eb-332db1133317 + +# run the build which kicks off a cache rebuild on the server +rsconnect content build run + +# once the build is complete, the content can be "untracked" +# this does not remove the content from the Connect server +# the entry is only removed from the local state file +rsconnect content build rm --guid 4ffc819c-065c-420c-88eb-332db1133317 +``` + +> **Note** +> See [this section](#add-to-build-from-search-results) for +> an example of how to add multiple content items in bulk, from the results +> of a `rsconnect content search` command. + +To view all currently "tracked" content items, use the `rsconnect content build ls` subcommand. + +```bash +rsconnect content build ls +``` + +To view only the "tracked" content items that have not yet been built, use the `--status NEEDS_BUILD` flag. + +```bash +rsconnect content build ls --status NEEDS_BUILD +``` + +Once the content items have been added, you may initiate a build +using the `rsconnect content build run` subcommand. This command will attempt to +build all "tracked" content that has the status `NEEDS_BUILD`. + +> To re-run failed builds, use `rsconnect content build run --retry`. This will build +all tracked content in any of the following states: `[NEEDS_BUILD, ABORTED, ERROR, RUNNING]`. +> +> If you encounter an error indicating that a build operation is already in progress, +you can use `rsconnect content build run --force` to bypass the check and proceed with building content marked as `NEEDS_BUILD`. +Ensure no other build operation is actively running before using the `--force` option. + +```bash +rsconnect content build run +# [INFO] 2021-12-14T13:02:45-0500 Initializing ContentBuildStore for https://connect.example.org:3939 +# [INFO] 2021-12-14T13:02:45-0500 Starting content build (https://connect.example.org:3939)... +# [INFO] 2021-12-14T13:02:45-0500 Starting build: 4ffc819c-065c-420c-88eb-332db1133317 +# [INFO] 2021-12-14T13:02:50-0500 Running = 1, Pending = 0, Success = 0, Error = 0 +# [INFO] 2021-12-14T13:02:50-0500 Build succeeded: 4ffc819c-065c-420c-88eb-332db1133317 +# [INFO] 2021-12-14T13:02:55-0500 Running = 0, Pending = 0, Success = 1, Error = 0 +# [INFO] 2021-12-14T13:02:55-0500 1/1 content builds completed in 0:00:10 +# [INFO] 2021-12-14T13:02:55-0500 Success = 1, Error = 0 +# [INFO] 2021-12-14T13:02:55-0500 Content build complete. +``` + +Sometimes content builds will fail and require debugging by the publisher or administrator. +Use the `rsconnect content build ls` to identify content builds that resulted in errors +and inspect the build logs with the `rsconnect content build logs` subcommand. + +```bash +rsconnect content build ls --status ERROR +# [INFO] 2021-12-14T13:07:32-0500 Initializing ContentBuildStore for https://connect.example.org:3939 +# [ +# { +# "rsconnect_build_status": "ERROR", +# "last_deployed_time": "2021-12-02T18:09:11Z", +# "owner_guid": "edf26318-0027-4d9d-bbbb-54703ebb1855", +# "rsconnect_last_build_log": "/Users/david/code/posit/rsconnect-python/rsconnect-build/logs/connect_example_org_3939/4ffc819c-065c-420c-88eb-332db1133317/pZoqfBoi6BgpKde5.log", +# "guid": "4ffc819c-065c-420c-88eb-332db1133317", +# "rsconnect_build_task_result": { +# "user_id": 1, +# "error": "Cannot find compatible environment: no compatible Local environment with Python version 3.9.5", +# "code": 1, +# "finished": true, +# "result": { +# "data": "An error occurred while building the content", +# "type": "build-failed-error" +# }, +# "id": "pZoqfBoi6BgpKde5" +# }, +# "dashboard_url": "https://connect.example.org:3939/connect/#/apps/4ffc819c-065c-420c-88eb-332db1133317", +# "name": "logs-api-python", +# "title": "logs-api-python", +# "content_url": "https://connect.example.org:3939/content/4ffc819c-065c-420c-88eb-332db1133317/", +# "bundle_id": "141", +# "rsconnect_last_build_time": "2021-12-14T18:07:16Z", +# "created_time": "2021-07-19T19:17:32Z", +# "app_mode": "python-api" +# } +# ] + +rsconnect content build logs --guid 4ffc819c-065c-420c-88eb-332db1133317 +# [INFO] 2021-12-14T13:09:27-0500 Initializing ContentBuildStore for https://connect.example.org:3939 +# Building Python API... +# Cannot find compatible environment: no compatible Local environment with Python version 3.9.5 +# Task failed. Task exited with status 1. +``` + +Once a build for a piece of tracked content is complete, it can be safely removed from the list of "tracked" +content by using `rsconnect content build rm` command. This command accepts a `--guid` argument to specify +which piece of content to remove. Removing the content from the list of tracked content simply removes the item +from the local state file, the content deployed to the server remains unchanged. + +```bash +rsconnect content build rm --guid 4ffc819c-065c-420c-88eb-332db1133317 +``` + +### Rebuilding lots of content + +When attempting to rebuild a long list of content, it is recommended to first build a sub-set of the content list. +First choose 1 or 2 Python and R content items for each version of Python and R on the server. Try to choose content +items that have the most dependencies in common with other content items on the server. Build these content items +first with the `rsconnect content build run` command. This will "warm" the Python and R environment cache for subsequent +content builds. Once these initial builds are complete, add the remaining content items to the list of "tracked" content +and execute another `rsconnect content build run` command. + +To execute multiple content builds simultaniously, use the `rsconnect content build run --parallelism` flag to increase the +number of concurrent builds. By default, each content item is built serially. Increasing the build parallelism can reduce the total +time needed to rebuild a long list of content items. We recommend starting with a low parallelism setting (2-3) and increasing +from there to avoid overloading the Connect server with concurrent build operations. Remember that these builds are executing on the +Connect server which consumes CPU, RAM, and i/o bandwidth that would otherwise we allocated for Python and R applications +running on the server. + +## Common Usage Examples + +### Searching for content + +The following are some examples of how publishers might use the +`rsconnect content search` subcommand to find content on Posit Connect. +By default, the `rsconnect content search` command will return metadata for ALL +of the content on a Posit Connect server, both published and unpublished content. + +> **Note** +> When using the `--r-version` and `--py-version` flags, users should +> make sure to quote the arguments to avoid conflicting with your shell. For +> example, bash would interpret `--py-version >3.0.0` as a shell redirect because of the +> unquoted `>` character. + +```bash +# return only published content +rsconnect content search --published + +# return only unpublished content +rsconnect content search --unpublished + +# return published content where the python version is at least 3.9.0 +rsconnect content search --published --py-version ">=3.9.0" + +# return published content where the R version is exactly 3.6.3 +rsconnect content search --published --r-version "==3.6.3" + +# return published content where the content type is a static RMD +rsconnect content search --content-type rmd-static + +# return published content where the content type is either shiny OR fast-api +rsconnect content search --content-type shiny --content-type python-fastapi + +# return all content, published or unpublished, where the title contains the +# text "Stock Report" +rsconnect content search --title-contains "Stock Report" + +# return published content, results are ordered by when the content was last +# deployed +rsconnect content search --published --order-by last_deployed + +# return published content, results are ordered by when the content was +# created +rsconnect content search --published --order-by created +``` + +### Finding r and python versions + +One common use for the `search` command might be to find the versions of +r and python that are currently in use on your Posit Connect server before a migration. + +```bash +# search for all published content and print the unique r and python version +# combinations +rsconnect content search --published | jq -c '.[] | {py_version,r_version}' | sort | +uniq +# {"py_version":"3.8.2","r_version":"3.5.3"} +# {"py_version":"3.8.2","r_version":"3.6.3"} +# {"py_version":"3.8.2","r_version":null} +# {"py_version":null,"r_version":"3.5.3"} +# {"py_version":null,"r_version":"3.6.3"} +# {"py_version":null,"r_version":null} +``` + +### Finding recently deployed content + +```bash +# return only the 10 most recently deployed content items +rsconnect content search \ + --order-by last_deployed \ + --published | jq -c 'limit(10; .[]) | { guid, last_deployed_time }' +# {"guid":"4ffc819c-065c-420c-88eb-332db1133317","last_deployed_time":"2021-12-02T18:09:11Z"} +# {"guid":"aa2603f8-1988-484f-a335-193f2c57e6c4","last_deployed_time":"2021-12-01T20:56:07Z"} +# {"guid":"051252f0-4f70-438f-9be1-d818a3b5f8d9","last_deployed_time":"2021-12-01T20:37:01Z"} +# {"guid":"015143da-b75f-407c-81b1-99c4a724341e","last_deployed_time":"2021-11-30T16:56:21Z"} +# {"guid":"bcc74209-3a81-4b9c-acd5-d24a597c256c","last_deployed_time":"2021-11-30T15:51:07Z"} +# {"guid":"f21d7767-c99e-4dd4-9b00-ff8ec9ae2f53","last_deployed_time":"2021-11-23T18:46:28Z"} +# {"guid":"da4f709c-c383-4fbc-89e2-f032b2d7e91d","last_deployed_time":"2021-11-23T18:46:28Z"} +# {"guid":"9180809d-38fd-4730-a0e0-8568c45d87b7","last_deployed_time":"2021-11-23T15:16:19Z"} +# {"guid":"2b1d2ab8-927d-4956-bbf9-29798d039bc5","last_deployed_time":"2021-11-22T18:33:17Z"} +# {"guid":"c96db3f3-87a1-4df5-9f58-eb109c397718","last_deployed_time":"2021-11-19T20:25:33Z"} +``` + +### Add to build from search results + +One common use case might be to `rsconnect content build add` content for build +based on the results of a `rsconnect content search`. For example: + +```bash +# search for all API type content, then +# for each guid, add it to the "tracked" content items +for guid in $(rsconnect content search \ + --published \ + --content-type python-api \ + --content-type api | jq -r '.[].guid'); do + rsconnect content build add --guid $guid +done +``` + +Adding content items one at a time can be a slow operation. This is because +`rsconnect content build add` must fetch metadata for each content item before it +is added to the "tracked" content items. By providing multiple `--guid` arguments +to the `rsconnect content build add` subcommand, we can fetch metadata for multiple content items +in a single api call, which speeds up the operation significantly. + +```bash +# write the guid of every published content item to a file called guids.txt +rsconnect content search --published | jq '.[].guid' > guids.txt + +# bulk-add from the guids.txt with a single `rsconnect content build add` command +xargs printf -- '-g %s\n' < guids.txt | xargs rsconnect content build add +``` +## Programmatic Provisioning + +Posit Connect supports the programmatic bootstrapping of an administrator API key +for scripted provisioning tasks. This process is supported by the `rsconnect bootstrap` command, +which uses a JSON Web Token to request an initial API key from a fresh Connect instance. + +```bash +rsconnect bootstrap \ + --server https://connect.example.org:3939 \ + --jwt-keypath /path/to/secret.key +``` + +A full description on how to use `rsconnect bootstrap` in a provisioning workflow is provided in the Connect administrator guide's +[programmatic provisioning](https://docs.posit.co/connect/admin/programmatic-provisioning) documentation. + +## Server Administration Tasks + +Starting with the 2023.05 edition of Posit Connect, `rsconnect-python` can be +used to perform certain server administration tasks, such as instance managing +runtime caches. For more information on runtime caches in Posit Connect, see the +Connect Admin Guide's section on [runtime +caches](https://docs.posit.co/connect/admin/server-management/runtime-caches/). + +Examples in this section will use `--name myserver` to stand in for your Connect +server information. See [Managing Server +Information](#managing-server-information) above for more details. + +### Enumerate Runtime Caches +*New in Connect 2023.05* + +Use the command below to enumerate runtime caches on a Connect server. The +command will output a JSON object containing a list of runtime caches . Each +cache entry will contain the following information: + +- `language`: The language of content that uses the cache, either R or Python. +- `version`: The language version of the content that uses the cache. +- `image_name`: The execution environment of the cache. The string `Local` + denotes native execution. For Connect instances that use off-host execution, + the name of the image that uses the cache will be displayed. + +```bash +rsconnect system caches list --name myserver +# { +# "caches": [ +# { +# "language": "R", +# "version": "3.6.3", +# "image_name": "Local" +# }, +# { +# "language": "Python", +# "version": "3.9.5", +# "image_name": "Local" +# }, +# { +# "language": "R", +# "version": "3.6.3", +# "image_name": "rstudio/content-base:r3.6.3-py3.9.5-bionic" +# }, +# { +# "language": "Python", +# "version": "3.9.5", +# "image_name": "rstudio/content-base:r3.6.3-py3.9.5-bionic" +# } +# ] +# } +``` + +> **Note** +> The `image_name` field returned by the server will use sanitized versions +> of names. + +### Delete Runtime Caches +*New in Connect 2023.05* + +When Connect's execution environment changes, runtime caches may be invalidated. +In these cases, you will need to delete the affected runtime caches using the +`system caches delete` command. + +> **Warning** +> After deleting a cache, the first time affected content is visited, Connect +> will need to reconstruct its environment. This can take a long time. To +> mitigate this, you can use the [`content build`](#content-build) command to +> rebuild affected content ahead of time. You may want to do this just for +> high-priority content, or for all content. + +To delete a runtime cache, call the `system caches delete` command, specifying a +Connect server, as well as the language (`-l, --language`), version (`-V, +--version`), and image name (`-I, --image-name`) for the cache you wish to +delete. Deleting a large cache might take a while. The command will wait for +Connect to finish the task. + +Use the following parameters specify the target cache: + +- `language` (required) must name `R` or `Python`. It is case-insensitive. +- `version` (required) must be a three-part version number, e.g. `3.8.12`. +- `image-name` (optional) defaults to `Local`, which targets caches used for + natively-executed content. Off-host images can be specified using either the + literal image name or the sanitized name returned by the `list` command. + +Use the dry run flag (`-d, --dry-run`) to surface any errors ahead of +deletion. + +```bash +rsconnect system caches delete \ + --name myserver \ + --language Python \ + --version 3.9.5 \ + --image-name rstudio/content-base:r3.6.3-py3.9.5-bionic \ + --dry-run +# Dry run finished + +rsconnect system caches delete \ + --name myserver \ + --language Python \ + --version 3.9.5 \ + --image-name rstudio/content-base:r3.6.3-py3.9.5-bionic +# Deleting runtime cache... +# Successfully deleted runtime cache +``` + +You should run these commands for each cache you wish to delete. diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml deleted file mode 100644 index 67b888ce..00000000 --- a/docs/mkdocs.yml +++ /dev/null @@ -1,55 +0,0 @@ -site_name: 'Posit Connect: rsconnect-python' -copyright: Posit Software, PBC. All Rights Reserved - -# We activate GA only when hosted on our public docs site -# and not when installed. -# -# See overrides/partials/integrations/analytics.html -google_analytics: - - 'GTM-KHBDBW7' - - 'auto' - -markdown_extensions: - - toc: - permalink: "#" - - attr_list: {} - - def_list: {} - - tables: {} - - admonition - - pymdownx.superfences: {} - - codehilite: - guess_lang: false - -plugins: - - macros - - search - -nav: - - index.md - - changelog.md - -theme: - name: material - custom_dir: overrides - font: - text: Open Sans - logo: 'images/iconPositConnect.svg' - favicon: 'images/favicon.ico' - palette: - - scheme: default - primary: white - toggle: - icon: material/toggle-switch-off-outline - name: Switch to dark mode - - scheme: slate - primary: black - toggle: - icon: material/toggle-switch - name: Switch to light mode - -extra_css: - - css/custom.css - -extra: - rsconnect_python: - version: !!python/object/apply:os.getenv ["VERSION"] diff --git a/mkdocs.yml b/mkdocs.yml new file mode 100644 index 00000000..a6a1c4e9 --- /dev/null +++ b/mkdocs.yml @@ -0,0 +1,87 @@ +site_name: 'Posit Connect: rsconnect-python' +copyright: Posit Software, PBC. All Rights Reserved + +markdown_extensions: + - attr_list + - mkdocs-click + - admonition + - footnotes + - pymdownx.details + - pymdownx.inlinehilite + - pymdownx.magiclink + - pymdownx.tasklist: + custom_checkbox: true + - pymdownx.snippets: + base_path: "docs/" + - pymdownx.highlight + - pymdownx.superfences: + custom_fences: + - name: mermaid + class: mermaid + format: !!python/name:pymdownx.superfences.fence_code_format + - meta + - toc: + permalink: "#" + - pymdownx.tabbed: + alternate_style: true + - pymdownx.emoji + - pymdownx.keys + - md_in_html + +plugins: + - macros + - search + +nav: + - Home: index.md + - CLI reference: + - rsconnect: + - add: commands/add.md + - bootstrap: commands/bootstrap.md + - content: commands/content.md + - deploy: commands/deploy.md + - details: commands/details.md + - info: commands/info.md + - list: commands/list.md + - remove: commands/remove.md + - system: commands/system.md + - version: commands/version.md + - write-manifest: commands/write-manifest.md + + +theme: + features: + - navigation.expand + name: material + custom_dir: docs/overrides + font: + text: Open Sans + logo: 'images/iconPositConnect.svg' + favicon: 'images/favicon.ico' + palette: + - media: "(prefers-color-scheme)" + toggle: + icon: material/brightness-auto + name: Switch to light mode + primary: white + accent: blue + - scheme: default + primary: white + toggle: + icon: material/toggle-switch-off-outline + name: Switch to dark mode + - scheme: slate + primary: black + toggle: + icon: material/toggle-switch + name: Switch to light mode + +extra_css: + - docs/css/custom.css + +extra: + rsconnect_python: + version: !!python/object/apply:os.getenv ["VERSION"] + analytics: + provider: google + property: 'GTM-KHBDBW7' diff --git a/pyproject.toml b/pyproject.toml index 45ef703f..374bfaba 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,6 +38,12 @@ test = [ "twine", "types-Flask", ] +docs = [ + "mkdocs-material", + "mkdocs-click", + "pymdown-extensions", + "mkdocs-macros-plugin" +] [project.urls] Repository = "http://github.com/posit-dev/rsconnect-python" From 41f655fc95d83060d12f4ef10d8fb4bf42c5c575 Mon Sep 17 00:00:00 2001 From: edavidaja Date: Fri, 2 May 2025 16:15:08 -0400 Subject: [PATCH 2/4] preview docs on pr --- .github/workflows/preview-docs.yml | 34 ++++++++++++++++++++++++++++++ .gitignore | 1 + 2 files changed, 35 insertions(+) create mode 100644 .github/workflows/preview-docs.yml diff --git a/.github/workflows/preview-docs.yml b/.github/workflows/preview-docs.yml new file mode 100644 index 00000000..ebdf47b2 --- /dev/null +++ b/.github/workflows/preview-docs.yml @@ -0,0 +1,34 @@ +name: preview docs + +on: + pull_request: + types: + - opened + - reopened + - synchronize + - closed + workflow_dispatch: + +concurrency: preview-${{ github.ref }} + +jobs: + deploy-preview: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - uses: actions/setup-python@v4 + with: + python-version: 3.x + + - name: Install and Build + if: github.event.action != 'closed' # You might want to skip the build if the PR has been closed + run: | + python -m pip install -e ".[docs]" + mkdocs build + + - name: Deploy preview + uses: rossjrw/pr-preview-action@v1 + with: + source-dir: ./site/ diff --git a/.gitignore b/.gitignore index 03f7ccdd..e2d57822 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,4 @@ vetiver-testing/rsconnect_api_keys.json # license files should not be commited to this repository *.lic +/site/ From 3164d0477e92d08f0a9322250d6da4459eed3689 Mon Sep 17 00:00:00 2001 From: "E. David Aja" Date: Fri, 2 May 2025 17:59:07 -0400 Subject: [PATCH 3/4] readme breakup (#665) --- docs/deploying.md | 470 +++++++++++++ docs/index.md | 1014 +---------------------------- docs/programmatic-provisioning.md | 14 + docs/server-administration.md | 517 +++++++++++++++ mkdocs.yml | 7 +- 5 files changed, 1008 insertions(+), 1014 deletions(-) create mode 100644 docs/deploying.md create mode 100644 docs/programmatic-provisioning.md create mode 100644 docs/server-administration.md diff --git a/docs/deploying.md b/docs/deploying.md new file mode 100644 index 00000000..51c197e6 --- /dev/null +++ b/docs/deploying.md @@ -0,0 +1,470 @@ +### Notebook Deployment Options + +There are a variety of options available to you when deploying a Jupyter notebook to +Posit Connect. + +#### Including Extra Files + +You can include extra files in the deployment bundle to make them available when your +notebook is run by the Posit Connect server. Just specify them on the command line +after the notebook file: + +```bash +rsconnect deploy notebook my-notebook.ipynb data.csv +``` + +#### Package Dependencies + +If a `requirements.txt` file exists in the same directory as the notebook file, it will +be included in the bundle. It must specify the package dependencies needed to execute +the notebook. Posit Connect will reconstruct the Python environment using the +specified package list. + +If there is no `requirements.txt` file or the `--force-generate` option is specified, +the package dependencies will be determined from the current Python environment, or +from an alternative Python executable specified via the `--python` option: + +```bash +rsconnect deploy notebook --python /path/to/python my-notebook.ipynb +``` + +You can see the packages list that will be included by running `pip list --format=freeze` yourself, +ensuring that you use the same Python that you use to run your Jupyter Notebook: + +```bash +/path/to/python -m pip list --format=freeze +``` + +#### Python Version + +When deploying Python content to Posit Connect, +the server will require a version of Python that matches the content +requirements. + +For example, a server with only Python 3.9 installed will fail to match content +that requires Python 3.8. + +`rsconnect` supports detecting Python version requirements in several ways: + 1. A `.python-version` file exists. In such case + `rsconnect` will use its content to determine the python version requirement. + 2. A `pyproject.toml` with a `project.requires-python` field exists. + In such case the requirement specified in the field will be used + if no `.python-version` file exists. + 3. A `setup.cfg` with an `options.python_requires` field exists. + In such case the requirement specified in the field will be used + if **1** or **2** were not already satisfied. + 4. If no other source of version requirement was found, then + the interpreter in use is considered the one required to run the content. + +On Posit Connect `>=2025.03.0` the requirement detected by `rsconnect` is +always respected. Older Connect versions will instead rely only on the +python version used to deploy the content to determine the requirement. + +For more information see the [Posit Connect Admin Guide chapter titled Python Version +Matching](https://docs.posit.co/connect/admin/python/#python-version-matching). + +We recommend providing a `pyproject.toml` with a `project.requires-python` field +if the deployed content is an installable package and a `.python-version` file +for plain directories. + +> **Note** +> The packages and package versions listed in `requirements.txt` must be +> compatible with the Python version you request. + +#### Static (Snapshot) Deployment + +By default, `rsconnect` deploys the original notebook with all its source code. This +enables the Posit Connect server to re-run the notebook upon request or on a schedule. + +If you just want to publish an HTML snapshot of the notebook, you can use the `--static` +option. This will cause `rsconnect` to execute your notebook locally to produce the HTML +file, then publish the HTML file to the Posit Connect server: + +```bash +rsconnect deploy notebook --static my-notebook.ipynb +``` + +### Creating a Manifest for Future Deployment + +You can create a `manifest.json` file for a Jupyter Notebook, then use that manifest +in a later deployment. Use the `write-manifest` command to do this. + +The `write-manifest` command will also create a `requirements.txt` file, if it does +not already exist or the `--force-generate` option is specified. It will contain the +package dependencies from the current Python environment, or from an alternative +Python executable specified in the `--python` option. + +Here is an example of the `write-manifest` command: + +```bash +rsconnect write-manifest notebook my-notebook.ipynb +``` + +> **Note** +> Manifests for static (pre-rendered) notebooks cannot be created. + +### API/Application Deployment Options + +You can deploy a variety of APIs and applications using sub-commands of the +`rsconnect deploy` command. + +* `api`: WSGI-compliant APIs (e.g., `bottle`, `falcon`, `flask`, `flask-restx`, `flasgger`, `pycnic`). +* `flask`: Flask APIs (_Note: `flask` is an alias of `api`._). +* `fastapi`: ASGI-compliant APIs (e.g, `fastapi`, `quart`, `sanic`, `starlette`) +* `dash`: Python Dash apps +* `streamlit`: Streamlit apps +* `bokeh`: Bokeh server apps +* `gradio`: Gradio apps + +All options below apply equally to the `api`, `fastapi`, `dash`, `streamlit`, +`gradio`, and `bokeh` sub-commands. + +#### Including Extra Files + +You can include extra files in the deployment bundle to make them available when your +API or application is run by the Posit Connect server. Just specify them on the +command line after the API or application directory: + +```bash +rsconnect deploy api flask-api/ data.csv +``` + +Since deploying an API or application starts at a directory level, there will be times +when some files under that directory subtree should not be included in the deployment +or manifest. Use the `--exclude` option to specify files or directories to exclude. + +```bash +rsconnect deploy dash --exclude dash-app-venv --exclude TODO.txt dash-app/ +``` + +You can exclude a directory by naming it: + +```bash +rsconnect deploy dash --exclude dash-app-venv --exclude output/ dash-app/ +``` + +The `--exclude` option may be repeated, and may include a glob pattern. +You should always quote a glob pattern so that it will be passed to `rsconnect` as-is +instead of letting the shell expand it. If a file is specifically listed as an extra +file that also matches an exclusion pattern, the file will still be included in the +deployment (i.e., extra files take precedence). + +```bash +rsconnect deploy dash --exclude dash-app-venv --exclude “*.txt” dash-app/ +``` + +The following shows an example of an extra file taking precedence: + +```bash +rsconnect deploy dash --exclude “*.csv” dash-app/ important_data.csv +``` + +The "`**`" glob pattern will recursively match all files and directories, +while "`*`" only matches files. The "`**`" pattern is useful with complicated +project hierarchies where enumerating the _included_ files is simpler than +listing the _exclusions_. + +```bash +rsconnect deploy quarto . _quarto.yml index.qmd requirements.txt --exclude "**" +``` + +Some directories are excluded by default, to prevent bundling and uploading files that are not needed or might interfere with the deployment process: + +``` +.Rproj.user +.env +.git +.svn +.venv +__pycache__ +env +packrat +renv +rsconnect-python +rsconnect +venv +``` + +Any directory that appears to be a Python virtual environment (by containing +`bin/python`) will also be excluded. + +#### Package Dependencies + +If a `requirements.txt` file exists in the API/application directory, it will be +included in the bundle. It must specify the package dependencies needed to execute +the API or application. Posit Connect will reconstruct the Python environment using +the specified package list. + +If there is no `requirements.txt` file or the `--force-generate` option is specified, +the package dependencies will be determined from the current Python environment, or +from an alternative Python executable specified via the `--python` option: + +```bash +rsconnect deploy api --python /path/to/python my-api/ +``` + +You can see the packages list that will be included by running `pip list --format=freeze` yourself, +ensuring that you use the same Python that you use to run your API or application: + +```bash +/path/to/python -m pip list --format=freeze +``` + +#### Python Version + +When deploying Python content to Posit Connect, +the server will require matching `` versions of Python. For example, +a server with only Python 3.9 installed will fail to match content deployed with +Python 3.8. Your administrator may also enable exact Python version matching which +will be stricter and require matching major, minor, and patch versions. For more +information see the [Posit Connect Admin Guide chapter titled Python Version +Matching](https://docs.posit.co/connect/admin/python/#python-version-matching). + +We recommend installing a version of Python on your client that is also available +in your Connect installation. If that's not possible, you can override +rsconnect-python's detected Python version and request a version of Python +that is installed in Connect, For example, this command: + +```bash +rsconnect deploy api --override-python-version 3.11.5 my-api/ +``` + +will deploy the content in `my-api` while requesting that Connect +use Python version 3.11.5. + +> **Note** +> The packages and package versions listed in `requirements.txt` must be +> compatible with the Python version you request. + +### Creating a Manifest for Future Deployment + +You can create a `manifest.json` file for an API or application, then use that +manifest in a later deployment. Use the `write-manifest` command to do this. + +The `write-manifest` command will also create a `requirements.txt` file, if it does +not already exist or the `--force-generate` option is specified. It will contain +the package dependencies from the current Python environment, or from an alternative +Python executable specified in the `--python` option. + +Here is an example of the `write-manifest` command: + +```bash +rsconnect write-manifest api my-api/ +``` + +### Deploying R or Other Content + +You can deploy other content that has an existing Posit Connect `manifest.json` +file. For example, if you download and unpack a source bundle from Posit Connect, +you can deploy the resulting directory. The options are similar to notebook or +API/application deployment; see `rsconnect deploy manifest --help` for details. + +Here is an example of the `deploy manifest` command: + +```bash +rsconnect deploy manifest /path/to/manifest.json +``` + +> **Note** +> In this case, the existing content is deployed as-is. Python environment +> inspection and notebook pre-rendering, if needed, are assumed to be done already +> and represented in the manifest. + +The argument to `deploy manifest` may also be a directory so long as that directory +contains a `manifest.json` file. + +If you have R content but don't have a `manifest.json` file, you can use the RStudio +IDE to create the manifest. See the help for the `rsconnect::writeManifest` R function: + +```r +install.packages('rsconnect') +library(rsconnect) +?rsconnect::writeManifest +``` + +### Options for All Types of Deployments + +These options apply to any type of content deployment. + +#### Title + +The title of the deployed content is, by default, derived from the filename. For +example, if you deploy `my-notebook.ipynb`, the title will be `my-notebook`. To change +this, use the `--title` option: + +``` +rsconnect deploy notebook --title "My Notebook" my-notebook.ipynb +``` + +When using `rsconnect deploy api`, `rsconnect deploy fastapi`, `rsconnect deploy dash`, +`rsconnect deploy streamlit`, `rsconnect deploy bokeh`, or `rsconnect deploy gradio`, +the title is derived from the directory containing the API or application. + +When using `rsconnect deploy manifest`, the title is derived from the primary +filename referenced in the manifest. + +#### Verification After Deployment + +After deploying your content, rsconnect accesses the deployed content +to verify that the deployment is live. This is done with a `GET` request +to the content, without parameters. The request is +considered successful if there isn't a 5xx code returned. Errors like +400 Bad Request or 405 Method Not Allowed because not all apps support `GET /`. +For cases where this is not desired, use the `--no-verify` flag on the command line. + +### Environment variables + +You can set environment variables during deployment. Their names and values will be +passed to Posit Connect during deployment so you can use them in your code. Note that +if you are using `rsconnect` to deploy to shinyapps.io, environment variable management +is not supported on that platform. + +For example, if `notebook.ipynb` contains + +```python +print(os.environ["MYVAR"]) +``` + +You can set the value of `MYVAR` that will be set when your code runs in Posit Connect +using the `-E/--environment` option: + +```bash +rsconnect deploy notebook --environment MYVAR='hello world' notebook.ipynb +``` + +To avoid exposing sensitive values on the command line, you can specify +a variable without a value. In this case, it will use the value from the +environment in which rsconnect-python is running: + +```bash +export SECRET_KEY=12345 + +rsconnect deploy notebook --environment SECRET_KEY notebook.ipynb +``` + +If you specify environment variables when updating an existing deployment, +new values will be set for the variables you provided. Other variables will +remain unchanged. If you don't specify any variables, all of the existing +variables will remain unchanged. + +Environment variables are set on the content item before the content bundle +is uploaded and deployed. If the deployment fails, the new environment variables +will still take effect. + + +### Updating a Deployment + +If you deploy a file again to the same server, `rsconnect` will update the previous +deployment. This means that you can keep running `rsconnect deploy notebook my-notebook.ipynb` +as you develop new versions of your notebook. The same applies to other Python content +types. + +#### Forcing a New Deployment + +To bypass this behavior and force a new deployment, use the `--new` option: + +```bash +rsconnect deploy dash --new my-app/ +``` + +#### Updating a Different Deployment + +If you want to update an existing deployment but don't have the saved deployment data, +you can provide the app's numeric ID or GUID on the command line: + +```bash +rsconnect deploy notebook --app-id 123456 my-notebook.ipynb +``` + +You must be the owner of the target deployment, or a collaborator with permission to +change the content. The type of content (static notebook, notebook with source code, +API, or application) must match the existing deployment. + +> **Note** +> There is no confirmation required to update a deployment. If you do so +> accidentally, use the "Source Versions" dialog in the Posit Connect dashboard to +> activate the previous version and remove the erroneous one. + +##### Finding the App ID + +The App ID associated with a piece of content you have previously deployed from the +`rsconnect` command line interface can be found easily by querying the deployment +information using the `info` command. For more information, see the +[Showing the Deployment Information](#showing-the-deployment-information) section. + +If the content was deployed elsewhere or `info` does not return the correct App ID, +but you can open the content on Posit Connect, find the content and open it in a +browser. The URL in your browser's location bar will contain `#/apps/NNN` where `NNN` +is your App ID. The GUID identifier for the app may be found on the **Info** tab for +the content in the Posit Connect UI. + +#### Showing the Deployment Information + +You can see the information that the `rsconnect` command has saved for the most recent +deployment with the `info` command: + +```bash +rsconnect info my-notebook.ipynb +``` + +If you have deployed to multiple servers, the most recent deployment information for +each server will be shown. This command also displays the path to the file where the +deployment data is stored. + +## Stored Information Files + +Stored information files are stored in a platform-specific directory: + +| Platform | Location | +| -------- | ------------------------------------------------------------------ | +| Mac | `$HOME/Library/Application Support/rsconnect-python/` | +| Linux | `$HOME/.rsconnect-python/` or `$XDG_CONFIG_HOME/rsconnect-python/` | +| Windows | `$APPDATA/rsconnect-python` | + +Remembered server information is stored in the `servers.json` file in that directory. + +### Deployment Data + +After a deployment is completed, information about the deployment is saved +to enable later redeployment. This data is stored alongside the deployed file, +in an `rsconnect-python` subdirectory, if possible. If that location is not writable +during deployment, then the deployment data will be stored in the global configuration +directory specified above. + +
+Generated from rsconnect-python {{ rsconnect_python.version }} +
+ +### Hide Jupyter Notebook Input Code Cells + +You can render a Jupyter notebook without its corresponding input code cells by passing the '--hide-all-input' flag through the cli: + +```bash +rsconnect deploy notebook \ + --server https://connect.example.org \ + --api-key my-api-key \ + --hide-all-input \ + my-notebook.ipynb +``` + +To selectively hide input cells in a Jupyter notebook, you need to do two things: + +1. tag cells with the 'hide_input' tag, +2. then pass the ' --hide-tagged-input' flag through the cli: + +```bash +rsconnect deploy notebook \ + --server https://connect.example.org \ + --api-key my-api-key \ + --hide-tagged-input \ + my-notebook.ipynb +``` + +By default, rsconnect-python does not install Jupyter notebook-related depenencies. +To use these hide input features in rsconnect-python you need to install these extra dependencies: + +``` +notebook +nbformat +nbconvert>=5.6.1 +``` diff --git a/docs/index.md b/docs/index.md index 24dc83c2..32a6e10f 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,6 +1,6 @@ -# The rsconnect-python CLI +# `rsconnect` -This package provides a CLI (command-line interface) for interacting +This package provides a (command-line interface) CLI for interacting with and deploying to Posit Connect. Many types of content supported by Posit Connect may be deployed by this package, including WSGI-style APIs, Dash, Streamlit, Gradio, and Bokeh applications. @@ -90,23 +90,6 @@ of saving this information, making it usable by a simple nickname. > in the file as plain text so care must be taken to prevent any unauthorized access > to the server information file. -#### TLS Support and Posit Connect - -Usually, a Posit Connect server will be set up to be accessed in a secure manner, -using the `https` protocol rather than simple `http`. If Posit Connect is set up -with a self-signed certificate, you will need to include the `--insecure` flag on -all commands. If Posit Connect is set up to require a client-side certificate chain, -you will need to include the `--cacert` option that points to your certificate -authority (CA) trusted certificates file. Both of these options can be saved along -with the URL and API Key for a server. - -> **Note** -> When certificate information is saved for the server, the specified file -> is read and its _contents_ are saved under the server's nickname. If the CA file's -> contents are ever changed, you will need to add the server information again. - -See the [Network Options](#network-options) section for more details about these options. - #### Remembering Server Information Use the `add` command to store information about a Posit Connect server: @@ -179,354 +162,6 @@ that are installed on the server. You can also use nicknames with the `details` command if you want to verify that the stored information is still valid. -### Notebook Deployment Options - -There are a variety of options available to you when deploying a Jupyter notebook to -Posit Connect. - -#### Including Extra Files - -You can include extra files in the deployment bundle to make them available when your -notebook is run by the Posit Connect server. Just specify them on the command line -after the notebook file: - -```bash -rsconnect deploy notebook my-notebook.ipynb data.csv -``` - -#### Package Dependencies - -If a `requirements.txt` file exists in the same directory as the notebook file, it will -be included in the bundle. It must specify the package dependencies needed to execute -the notebook. Posit Connect will reconstruct the Python environment using the -specified package list. - -If there is no `requirements.txt` file or the `--force-generate` option is specified, -the package dependencies will be determined from the current Python environment, or -from an alternative Python executable specified via the `--python` option: - -```bash -rsconnect deploy notebook --python /path/to/python my-notebook.ipynb -``` - -You can see the packages list that will be included by running `pip list --format=freeze` yourself, -ensuring that you use the same Python that you use to run your Jupyter Notebook: - -```bash -/path/to/python -m pip list --format=freeze -``` - -#### Python Version - -When deploying Python content to Posit Connect, -the server will require a version of Python that matches the content -requirements. - -For example, a server with only Python 3.9 installed will fail to match content -that requires Python 3.8. - -`rsconnect` supports detecting Python version requirements in several ways: - 1. A `.python-version` file exists. In such case - `rsconnect` will use its content to determine the python version requirement. - 2. A `pyproject.toml` with a `project.requires-python` field exists. - In such case the requirement specified in the field will be used - if no `.python-version` file exists. - 3. A `setup.cfg` with an `options.python_requires` field exists. - In such case the requirement specified in the field will be used - if **1** or **2** were not already satisfied. - 4. If no other source of version requirement was found, then - the interpreter in use is considered the one required to run the content. - -On Posit Connect `>=2025.03.0` the requirement detected by `rsconnect` is -always respected. Older Connect versions will instead rely only on the -python version used to deploy the content to determine the requirement. - -For more information see the [Posit Connect Admin Guide chapter titled Python Version -Matching](https://docs.posit.co/connect/admin/python/#python-version-matching). - -We recommend providing a `pyproject.toml` with a `project.requires-python` field -if the deployed content is an installable package and a `.python-version` file -for plain directories. - -> **Note** -> The packages and package versions listed in `requirements.txt` must be -> compatible with the Python version you request. - - -#### Static (Snapshot) Deployment - -By default, `rsconnect` deploys the original notebook with all its source code. This -enables the Posit Connect server to re-run the notebook upon request or on a schedule. - -If you just want to publish an HTML snapshot of the notebook, you can use the `--static` -option. This will cause `rsconnect` to execute your notebook locally to produce the HTML -file, then publish the HTML file to the Posit Connect server: - -```bash -rsconnect deploy notebook --static my-notebook.ipynb -``` - -### Creating a Manifest for Future Deployment - -You can create a `manifest.json` file for a Jupyter Notebook, then use that manifest -in a later deployment. Use the `write-manifest` command to do this. - -The `write-manifest` command will also create a `requirements.txt` file, if it does -not already exist or the `--force-generate` option is specified. It will contain the -package dependencies from the current Python environment, or from an alternative -Python executable specified in the `--python` option. - -Here is an example of the `write-manifest` command: - -```bash -rsconnect write-manifest notebook my-notebook.ipynb -``` - -> **Note** -> Manifests for static (pre-rendered) notebooks cannot be created. - -### API/Application Deployment Options - -You can deploy a variety of APIs and applications using sub-commands of the -`rsconnect deploy` command. - -* `api`: WSGI-compliant APIs (e.g., `bottle`, `falcon`, `flask`, `flask-restx`, `flasgger`, `pycnic`). -* `flask`: Flask APIs (_Note: `flask` is an alias of `api`._). -* `fastapi`: ASGI-compliant APIs (e.g, `fastapi`, `quart`, `sanic`, `starlette`) -* `dash`: Python Dash apps -* `streamlit`: Streamlit apps -* `bokeh`: Bokeh server apps -* `gradio`: Gradio apps - -All options below apply equally to the `api`, `fastapi`, `dash`, `streamlit`, -`gradio`, and `bokeh` sub-commands. - -#### Including Extra Files - -You can include extra files in the deployment bundle to make them available when your -API or application is run by the Posit Connect server. Just specify them on the -command line after the API or application directory: - -```bash -rsconnect deploy api flask-api/ data.csv -``` - -Since deploying an API or application starts at a directory level, there will be times -when some files under that directory subtree should not be included in the deployment -or manifest. Use the `--exclude` option to specify files or directories to exclude. - -```bash -rsconnect deploy dash --exclude dash-app-venv --exclude TODO.txt dash-app/ -``` - -You can exclude a directory by naming it: -```bash -rsconnect deploy dash --exclude dash-app-venv --exclude output/ dash-app/ -``` - -The `--exclude` option may be repeated, and may include a glob pattern. -You should always quote a glob pattern so that it will be passed to `rsconnect` as-is -instead of letting the shell expand it. If a file is specifically listed as an extra -file that also matches an exclusion pattern, the file will still be included in the -deployment (i.e., extra files take precedence). - -```bash -rsconnect deploy dash --exclude dash-app-venv --exclude “*.txt” dash-app/ -``` - -The following shows an example of an extra file taking precedence: - -```bash -rsconnect deploy dash --exclude “*.csv” dash-app/ important_data.csv -``` - -The "`**`" glob pattern will recursively match all files and directories, -while "`*`" only matches files. The "`**`" pattern is useful with complicated -project hierarchies where enumerating the _included_ files is simpler than -listing the _exclusions_. - -```bash -rsconnect deploy quarto . _quarto.yml index.qmd requirements.txt --exclude "**" -``` - -Some directories are excluded by default, to prevent bundling and uploading files that are not needed or might interfere with the deployment process: - -``` -.Rproj.user -.env -.git -.svn -.venv -__pycache__ -env -packrat -renv -rsconnect-python -rsconnect -venv -``` - -Any directory that appears to be a Python virtual environment (by containing -`bin/python`) will also be excluded. - - -#### Package Dependencies - -If a `requirements.txt` file exists in the API/application directory, it will be -included in the bundle. It must specify the package dependencies needed to execute -the API or application. Posit Connect will reconstruct the Python environment using -the specified package list. - -If there is no `requirements.txt` file or the `--force-generate` option is specified, -the package dependencies will be determined from the current Python environment, or -from an alternative Python executable specified via the `--python` option: - -```bash -rsconnect deploy api --python /path/to/python my-api/ -``` - -You can see the packages list that will be included by running `pip list --format=freeze` yourself, -ensuring that you use the same Python that you use to run your API or application: - -```bash -/path/to/python -m pip list --format=freeze -``` - -#### Python Version - -When deploying Python content to Posit Connect, -the server will require matching `` versions of Python. For example, -a server with only Python 3.9 installed will fail to match content deployed with -Python 3.8. Your administrator may also enable exact Python version matching which -will be stricter and require matching major, minor, and patch versions. For more -information see the [Posit Connect Admin Guide chapter titled Python Version -Matching](https://docs.posit.co/connect/admin/python/#python-version-matching). - -We recommend installing a version of Python on your client that is also available -in your Connect installation. If that's not possible, you can override -rsconnect-python's detected Python version and request a version of Python -that is installed in Connect, For example, this command: - -```bash -rsconnect deploy api --override-python-version 3.11.5 my-api/ -``` - -will deploy the content in `my-api` while requesting that Connect -use Python version 3.11.5. - -> **Note** -> The packages and package versions listed in `requirements.txt` must be -> compatible with the Python version you request. - -### Creating a Manifest for Future Deployment - -You can create a `manifest.json` file for an API or application, then use that -manifest in a later deployment. Use the `write-manifest` command to do this. - -The `write-manifest` command will also create a `requirements.txt` file, if it does -not already exist or the `--force-generate` option is specified. It will contain -the package dependencies from the current Python environment, or from an alternative -Python executable specified in the `--python` option. - -Here is an example of the `write-manifest` command: - -```bash -rsconnect write-manifest api my-api/ -``` - -### Deploying R or Other Content - -You can deploy other content that has an existing Posit Connect `manifest.json` -file. For example, if you download and unpack a source bundle from Posit Connect, -you can deploy the resulting directory. The options are similar to notebook or -API/application deployment; see `rsconnect deploy manifest --help` for details. - -Here is an example of the `deploy manifest` command: - -```bash -rsconnect deploy manifest /path/to/manifest.json -``` - -> **Note** -> In this case, the existing content is deployed as-is. Python environment -> inspection and notebook pre-rendering, if needed, are assumed to be done already -> and represented in the manifest. - -The argument to `deploy manifest` may also be a directory so long as that directory -contains a `manifest.json` file. - -If you have R content but don't have a `manifest.json` file, you can use the RStudio -IDE to create the manifest. See the help for the `rsconnect::writeManifest` R function: - -```r -install.packages('rsconnect') -library(rsconnect) -?rsconnect::writeManifest -``` - -### Options for All Types of Deployments - -These options apply to any type of content deployment. - -#### Title - -The title of the deployed content is, by default, derived from the filename. For -example, if you deploy `my-notebook.ipynb`, the title will be `my-notebook`. To change -this, use the `--title` option: - -``` -rsconnect deploy notebook --title "My Notebook" my-notebook.ipynb -``` - -When using `rsconnect deploy api`, `rsconnect deploy fastapi`, `rsconnect deploy dash`, -`rsconnect deploy streamlit`, `rsconnect deploy bokeh`, or `rsconnect deploy gradio`, -the title is derived from the directory containing the API or application. - -When using `rsconnect deploy manifest`, the title is derived from the primary -filename referenced in the manifest. - -#### Verification After Deployment -After deploying your content, rsconnect accesses the deployed content -to verify that the deployment is live. This is done with a `GET` request -to the content, without parameters. The request is -considered successful if there isn't a 5xx code returned. Errors like -400 Bad Request or 405 Method Not Allowed because not all apps support `GET /`. -For cases where this is not desired, use the `--no-verify` flag on the command line. - -### Environment variables -You can set environment variables during deployment. Their names and values will be -passed to Posit Connect during deployment so you can use them in your code. Note that -if you are using `rsconnect` to deploy to shinyapps.io, environment variable management -is not supported on that platform. - -For example, if `notebook.ipynb` contains -```python -print(os.environ["MYVAR"]) -``` - -You can set the value of `MYVAR` that will be set when your code runs in Posit Connect -using the `-E/--environment` option: -```bash -rsconnect deploy notebook --environment MYVAR='hello world' notebook.ipynb -``` - -To avoid exposing sensitive values on the command line, you can specify -a variable without a value. In this case, it will use the value from the -environment in which rsconnect-python is running: -```bash -export SECRET_KEY=12345 - -rsconnect deploy notebook --environment SECRET_KEY notebook.ipynb -``` - -If you specify environment variables when updating an existing deployment, -new values will be set for the variables you provided. Other variables will -remain unchanged. If you don't specify any variables, all of the existing -variables will remain unchanged. - -Environment variables are set on the content item before the content bundle -is uploaded and deployed. If the deployment fails, the new environment variables -will still take effect. ### Network Options @@ -571,648 +206,3 @@ If this fails with a TLS Certificate Validation error, then you have two options Once you work out the combination of options that allow you to successfully work with an instance of Posit Connect, you'll probably want to use the `add` command to have `rsconnect` remember those options and allow you to just use a nickname. - -### Updating a Deployment - -If you deploy a file again to the same server, `rsconnect` will update the previous -deployment. This means that you can keep running `rsconnect deploy notebook my-notebook.ipynb` -as you develop new versions of your notebook. The same applies to other Python content -types. - -#### Forcing a New Deployment - -To bypass this behavior and force a new deployment, use the `--new` option: - -```bash -rsconnect deploy dash --new my-app/ -``` - -#### Updating a Different Deployment - -If you want to update an existing deployment but don't have the saved deployment data, -you can provide the app's numeric ID or GUID on the command line: - -```bash -rsconnect deploy notebook --app-id 123456 my-notebook.ipynb -``` - -You must be the owner of the target deployment, or a collaborator with permission to -change the content. The type of content (static notebook, notebook with source code, -API, or application) must match the existing deployment. - -> **Note** -> There is no confirmation required to update a deployment. If you do so -> accidentally, use the "Source Versions" dialog in the Posit Connect dashboard to -> activate the previous version and remove the erroneous one. - -##### Finding the App ID - -The App ID associated with a piece of content you have previously deployed from the -`rsconnect` command line interface can be found easily by querying the deployment -information using the `info` command. For more information, see the -[Showing the Deployment Information](#showing-the-deployment-information) section. - -If the content was deployed elsewhere or `info` does not return the correct App ID, -but you can open the content on Posit Connect, find the content and open it in a -browser. The URL in your browser's location bar will contain `#/apps/NNN` where `NNN` -is your App ID. The GUID identifier for the app may be found on the **Info** tab for -the content in the Posit Connect UI. - -#### Showing the Deployment Information - -You can see the information that the `rsconnect` command has saved for the most recent -deployment with the `info` command: - -```bash -rsconnect info my-notebook.ipynb -``` - -If you have deployed to multiple servers, the most recent deployment information for -each server will be shown. This command also displays the path to the file where the -deployment data is stored. - -## Stored Information Files - -Stored information files are stored in a platform-specific directory: - -| Platform | Location | -| -------- | ------------------------------------------------------------------ | -| Mac | `$HOME/Library/Application Support/rsconnect-python/` | -| Linux | `$HOME/.rsconnect-python/` or `$XDG_CONFIG_HOME/rsconnect-python/` | -| Windows | `$APPDATA/rsconnect-python` | - -Remembered server information is stored in the `servers.json` file in that directory. - -### Deployment Data - -After a deployment is completed, information about the deployment is saved -to enable later redeployment. This data is stored alongside the deployed file, -in an `rsconnect-python` subdirectory, if possible. If that location is not writable -during deployment, then the deployment data will be stored in the global configuration -directory specified above. - -
-Generated from rsconnect-python {{ rsconnect_python.version }} -
- -### Hide Jupyter Notebook Input Code Cells - -You can render a Jupyter notebook without its corresponding input code cells by passing the '--hide-all-input' flag through the cli: - -```bash -rsconnect deploy notebook \ - --server https://connect.example.org \ - --api-key my-api-key \ - --hide-all-input \ - my-notebook.ipynb -``` - -To selectively hide input cells in a Jupyter notebook, you need to do two things: - -1. tag cells with the 'hide_input' tag, -2. then pass the ' --hide-tagged-input' flag through the cli: - -```bash -rsconnect deploy notebook \ - --server https://connect.example.org \ - --api-key my-api-key \ - --hide-tagged-input \ - my-notebook.ipynb -``` - -By default, rsconnect-python does not install Jupyter notebook-related depenencies. -To use these hide input features in rsconnect-python you need to install these extra dependencies: - -``` -notebook -nbformat -nbconvert>=5.6.1 -``` - -## Content subcommands - -rsconnect-python supports multiple options for interacting with Posit Connect's -`/v1/content` API. Both administrators and publishers can use the content subcommands -to search, download, and rebuild content on Posit Connect without needing to access the -dashboard from a browser. - -> **Note** -> The `rsconnect content` CLI subcommands are intended to be easily scriptable. -> The default output format is `JSON` so that the results can be easily piped into -> other command line utilities like [`jq`](https://stedolan.github.io/jq/) for further post-processing. - -```bash -rsconnect content --help -# Usage: rsconnect content [OPTIONS] COMMAND [ARGS]... - -# Interact with Posit Connect's content API. - -# Options: -# --help Show this message and exit. - -# Commands: -# build Build content on Posit Connect. -# describe Describe a content item on Posit Connect. -# download-bundle Download a content item's source bundle. -# search Search for content on Posit Connect. -``` - -### Content Search - -The `rsconnect content search` subcommands can be used by administrators and publishers -to find specific content on a given Posit Connect server. The search returns -metadata for each content item that meets the search criteria. - -```bash -rsconnect content search --help -# Usage: rsconnect content search [OPTIONS] - -# Options: -# -n, --name TEXT The nickname of the Posit Connect server. -# -s, --server TEXT The URL for the Posit Connect server. -# -k, --api-key TEXT The API key to use to authenticate with -# Posit Connect. - -# -i, --insecure Disable TLS certification/host validation. -# -c, --cacert FILENAME The path to trusted TLS CA certificates. -# --published Search only published content. -# --unpublished Search only unpublished content. -# --content-type [unknown|shiny|rmd-static|rmd-shiny|static|api|tensorflow-saved-model|jupyter-static|python-api|python-dash|python-streamlit|python-bokeh|python-fastapi|python-gradio|quarto-shiny|quarto-static] -# Filter content results by content type. -# --r-version VERSIONSEARCHFILTER -# Filter content results by R version. -# --py-version VERSIONSEARCHFILTER -# Filter content results by Python version. -# --title-contains TEXT Filter content results by title. -# --order-by [created|last_deployed] -# Order content results. -# -v, --verbose Print detailed messages. -# --help Show this message and exit. - -rsconnect content search -# [ -# { -# "max_conns_per_process": null, -# "content_category": "", -# "load_factor": null, -# "cluster_name": "Local", -# "description": "", -# "bundle_id": "142", -# "image_name": null, -# "r_version": null, -# "content_url": "https://connect.example.org:3939/content/4ffc819c-065c-420c-88eb-332db1133317/", -# "connection_timeout": null, -# "min_processes": null, -# "last_deployed_time": "2021-12-02T18:09:11Z", -# "name": "logs-api-python", -# "title": "logs-api-python", -# "created_time": "2021-07-19T19:17:32Z", -# "read_timeout": null, -# "guid": "4ffc819c-065c-420c-88eb-332db1133317", -# "parameterized": false, -# "run_as": null, -# "py_version": "3.8.2", -# "idle_timeout": null, -# "app_role": "owner", -# "access_type": "acl", -# "app_mode": "python-api", -# "init_timeout": null, -# "id": "18", -# "quarto_version": null, -# "dashboard_url": "https://connect.example.org:3939/connect/#/apps/4ffc819c-065c-420c-88eb-332db1133317", -# "run_as_current_user": false, -# "owner_guid": "edf26318-0027-4d9d-bbbb-54703ebb1855", -# "max_processes": null -# }, -# ... -# ] -``` - -See [this section](#searching-for-content) for more comprehensive usage examples -of the available search flags. - - -### Content Build - -> **Note** -> The `rsconnect content build` subcommand requires Posit Connect >= 2021.11.1 - -Posit Connect caches R and Python packages in the configured -[`Server.DataDir`](https://docs.posit.co/connect/admin/appendix/configuration/#Server.DataDir). -Under certain circumstances (examples below), these package caches can become stale -and need to be rebuilt. This refresh automatically occurs when a Posit Connect -user visits the content. You may wish to refresh some content before it is visited -because it is high priority or is not visited frequently (API content, emailed reports). -In these cases, it is possible to preemptively build specific content items using -the `rsconnect content build` subcommands. This way the user does not have to pay -the build cost when the content is accessed next. - -The following are some common scenarios where performing a content build might be necessary: - -- OS upgrade -- changes to gcc or libc libraries -- changes to Python or R installations -- switching from source to binary package repositories or vice versa - -> **Note** -> The `content build` command is non-destructive, meaning that it does nothing to purge -> existing packrat/python package caches before a build. If you have an -> existing cache, it should be cleared prior to starting a content build. -> See the [migration documentation](https://docs.posit.co/connect/admin/appendix/cli/#migration) for details. - -> **Note** -> You may use the [`rsconnect content search`](#content-search) subcommand to help -> identify high priority content items to build. - -```bash -rsconnect content build --help -Usage: rsconnect content build [OPTIONS] COMMAND [ARGS]... - - Build content on Posit Connect. Requires Connect >= 2021.11.1 - -Options: - --help Show this message and exit. - -Commands: - add Mark a content item for build. Use `build run` to invoke the build - on the Connect server. - - history Get the build history for a content item. - logs Print the logs for a content build. - ls List the content items that are being tracked for build on a given - Connect server. - - rm Remove a content item from the list of content that are tracked for - build. Use `build ls` to view the tracked content. - - run Start building content on a given Connect server. -``` - -To build a specific content item, first `add` it to the list of content that is -"tracked" for building using its GUID. Content that is "tracked" in the local state -may become out-of-sync with what exists remotely on the Connect server (the result of -`rsconnect content search`). When this happens, it is safe to remove the locally tracked -entries with `rsconnect content build rm`. - -> **Note** -> Metadata for "tracked" content items is stored in a local directory called -> `rsconnect-build` which will be automatically created in your current working directory. -> You may set the environment variable `CONNECT_CONTENT_BUILD_DIR` to override this directory location. - -```bash -# `add` the content to mark it as "tracked" -rsconnect content build add --guid 4ffc819c-065c-420c-88eb-332db1133317 - -# run the build which kicks off a cache rebuild on the server -rsconnect content build run - -# once the build is complete, the content can be "untracked" -# this does not remove the content from the Connect server -# the entry is only removed from the local state file -rsconnect content build rm --guid 4ffc819c-065c-420c-88eb-332db1133317 -``` - -> **Note** -> See [this section](#add-to-build-from-search-results) for -> an example of how to add multiple content items in bulk, from the results -> of a `rsconnect content search` command. - -To view all currently "tracked" content items, use the `rsconnect content build ls` subcommand. - -```bash -rsconnect content build ls -``` - -To view only the "tracked" content items that have not yet been built, use the `--status NEEDS_BUILD` flag. - -```bash -rsconnect content build ls --status NEEDS_BUILD -``` - -Once the content items have been added, you may initiate a build -using the `rsconnect content build run` subcommand. This command will attempt to -build all "tracked" content that has the status `NEEDS_BUILD`. - -> To re-run failed builds, use `rsconnect content build run --retry`. This will build -all tracked content in any of the following states: `[NEEDS_BUILD, ABORTED, ERROR, RUNNING]`. -> -> If you encounter an error indicating that a build operation is already in progress, -you can use `rsconnect content build run --force` to bypass the check and proceed with building content marked as `NEEDS_BUILD`. -Ensure no other build operation is actively running before using the `--force` option. - -```bash -rsconnect content build run -# [INFO] 2021-12-14T13:02:45-0500 Initializing ContentBuildStore for https://connect.example.org:3939 -# [INFO] 2021-12-14T13:02:45-0500 Starting content build (https://connect.example.org:3939)... -# [INFO] 2021-12-14T13:02:45-0500 Starting build: 4ffc819c-065c-420c-88eb-332db1133317 -# [INFO] 2021-12-14T13:02:50-0500 Running = 1, Pending = 0, Success = 0, Error = 0 -# [INFO] 2021-12-14T13:02:50-0500 Build succeeded: 4ffc819c-065c-420c-88eb-332db1133317 -# [INFO] 2021-12-14T13:02:55-0500 Running = 0, Pending = 0, Success = 1, Error = 0 -# [INFO] 2021-12-14T13:02:55-0500 1/1 content builds completed in 0:00:10 -# [INFO] 2021-12-14T13:02:55-0500 Success = 1, Error = 0 -# [INFO] 2021-12-14T13:02:55-0500 Content build complete. -``` - -Sometimes content builds will fail and require debugging by the publisher or administrator. -Use the `rsconnect content build ls` to identify content builds that resulted in errors -and inspect the build logs with the `rsconnect content build logs` subcommand. - -```bash -rsconnect content build ls --status ERROR -# [INFO] 2021-12-14T13:07:32-0500 Initializing ContentBuildStore for https://connect.example.org:3939 -# [ -# { -# "rsconnect_build_status": "ERROR", -# "last_deployed_time": "2021-12-02T18:09:11Z", -# "owner_guid": "edf26318-0027-4d9d-bbbb-54703ebb1855", -# "rsconnect_last_build_log": "/Users/david/code/posit/rsconnect-python/rsconnect-build/logs/connect_example_org_3939/4ffc819c-065c-420c-88eb-332db1133317/pZoqfBoi6BgpKde5.log", -# "guid": "4ffc819c-065c-420c-88eb-332db1133317", -# "rsconnect_build_task_result": { -# "user_id": 1, -# "error": "Cannot find compatible environment: no compatible Local environment with Python version 3.9.5", -# "code": 1, -# "finished": true, -# "result": { -# "data": "An error occurred while building the content", -# "type": "build-failed-error" -# }, -# "id": "pZoqfBoi6BgpKde5" -# }, -# "dashboard_url": "https://connect.example.org:3939/connect/#/apps/4ffc819c-065c-420c-88eb-332db1133317", -# "name": "logs-api-python", -# "title": "logs-api-python", -# "content_url": "https://connect.example.org:3939/content/4ffc819c-065c-420c-88eb-332db1133317/", -# "bundle_id": "141", -# "rsconnect_last_build_time": "2021-12-14T18:07:16Z", -# "created_time": "2021-07-19T19:17:32Z", -# "app_mode": "python-api" -# } -# ] - -rsconnect content build logs --guid 4ffc819c-065c-420c-88eb-332db1133317 -# [INFO] 2021-12-14T13:09:27-0500 Initializing ContentBuildStore for https://connect.example.org:3939 -# Building Python API... -# Cannot find compatible environment: no compatible Local environment with Python version 3.9.5 -# Task failed. Task exited with status 1. -``` - -Once a build for a piece of tracked content is complete, it can be safely removed from the list of "tracked" -content by using `rsconnect content build rm` command. This command accepts a `--guid` argument to specify -which piece of content to remove. Removing the content from the list of tracked content simply removes the item -from the local state file, the content deployed to the server remains unchanged. - -```bash -rsconnect content build rm --guid 4ffc819c-065c-420c-88eb-332db1133317 -``` - -### Rebuilding lots of content - -When attempting to rebuild a long list of content, it is recommended to first build a sub-set of the content list. -First choose 1 or 2 Python and R content items for each version of Python and R on the server. Try to choose content -items that have the most dependencies in common with other content items on the server. Build these content items -first with the `rsconnect content build run` command. This will "warm" the Python and R environment cache for subsequent -content builds. Once these initial builds are complete, add the remaining content items to the list of "tracked" content -and execute another `rsconnect content build run` command. - -To execute multiple content builds simultaniously, use the `rsconnect content build run --parallelism` flag to increase the -number of concurrent builds. By default, each content item is built serially. Increasing the build parallelism can reduce the total -time needed to rebuild a long list of content items. We recommend starting with a low parallelism setting (2-3) and increasing -from there to avoid overloading the Connect server with concurrent build operations. Remember that these builds are executing on the -Connect server which consumes CPU, RAM, and i/o bandwidth that would otherwise we allocated for Python and R applications -running on the server. - -## Common Usage Examples - -### Searching for content - -The following are some examples of how publishers might use the -`rsconnect content search` subcommand to find content on Posit Connect. -By default, the `rsconnect content search` command will return metadata for ALL -of the content on a Posit Connect server, both published and unpublished content. - -> **Note** -> When using the `--r-version` and `--py-version` flags, users should -> make sure to quote the arguments to avoid conflicting with your shell. For -> example, bash would interpret `--py-version >3.0.0` as a shell redirect because of the -> unquoted `>` character. - -```bash -# return only published content -rsconnect content search --published - -# return only unpublished content -rsconnect content search --unpublished - -# return published content where the python version is at least 3.9.0 -rsconnect content search --published --py-version ">=3.9.0" - -# return published content where the R version is exactly 3.6.3 -rsconnect content search --published --r-version "==3.6.3" - -# return published content where the content type is a static RMD -rsconnect content search --content-type rmd-static - -# return published content where the content type is either shiny OR fast-api -rsconnect content search --content-type shiny --content-type python-fastapi - -# return all content, published or unpublished, where the title contains the -# text "Stock Report" -rsconnect content search --title-contains "Stock Report" - -# return published content, results are ordered by when the content was last -# deployed -rsconnect content search --published --order-by last_deployed - -# return published content, results are ordered by when the content was -# created -rsconnect content search --published --order-by created -``` - -### Finding r and python versions - -One common use for the `search` command might be to find the versions of -r and python that are currently in use on your Posit Connect server before a migration. - -```bash -# search for all published content and print the unique r and python version -# combinations -rsconnect content search --published | jq -c '.[] | {py_version,r_version}' | sort | -uniq -# {"py_version":"3.8.2","r_version":"3.5.3"} -# {"py_version":"3.8.2","r_version":"3.6.3"} -# {"py_version":"3.8.2","r_version":null} -# {"py_version":null,"r_version":"3.5.3"} -# {"py_version":null,"r_version":"3.6.3"} -# {"py_version":null,"r_version":null} -``` - -### Finding recently deployed content - -```bash -# return only the 10 most recently deployed content items -rsconnect content search \ - --order-by last_deployed \ - --published | jq -c 'limit(10; .[]) | { guid, last_deployed_time }' -# {"guid":"4ffc819c-065c-420c-88eb-332db1133317","last_deployed_time":"2021-12-02T18:09:11Z"} -# {"guid":"aa2603f8-1988-484f-a335-193f2c57e6c4","last_deployed_time":"2021-12-01T20:56:07Z"} -# {"guid":"051252f0-4f70-438f-9be1-d818a3b5f8d9","last_deployed_time":"2021-12-01T20:37:01Z"} -# {"guid":"015143da-b75f-407c-81b1-99c4a724341e","last_deployed_time":"2021-11-30T16:56:21Z"} -# {"guid":"bcc74209-3a81-4b9c-acd5-d24a597c256c","last_deployed_time":"2021-11-30T15:51:07Z"} -# {"guid":"f21d7767-c99e-4dd4-9b00-ff8ec9ae2f53","last_deployed_time":"2021-11-23T18:46:28Z"} -# {"guid":"da4f709c-c383-4fbc-89e2-f032b2d7e91d","last_deployed_time":"2021-11-23T18:46:28Z"} -# {"guid":"9180809d-38fd-4730-a0e0-8568c45d87b7","last_deployed_time":"2021-11-23T15:16:19Z"} -# {"guid":"2b1d2ab8-927d-4956-bbf9-29798d039bc5","last_deployed_time":"2021-11-22T18:33:17Z"} -# {"guid":"c96db3f3-87a1-4df5-9f58-eb109c397718","last_deployed_time":"2021-11-19T20:25:33Z"} -``` - -### Add to build from search results - -One common use case might be to `rsconnect content build add` content for build -based on the results of a `rsconnect content search`. For example: - -```bash -# search for all API type content, then -# for each guid, add it to the "tracked" content items -for guid in $(rsconnect content search \ - --published \ - --content-type python-api \ - --content-type api | jq -r '.[].guid'); do - rsconnect content build add --guid $guid -done -``` - -Adding content items one at a time can be a slow operation. This is because -`rsconnect content build add` must fetch metadata for each content item before it -is added to the "tracked" content items. By providing multiple `--guid` arguments -to the `rsconnect content build add` subcommand, we can fetch metadata for multiple content items -in a single api call, which speeds up the operation significantly. - -```bash -# write the guid of every published content item to a file called guids.txt -rsconnect content search --published | jq '.[].guid' > guids.txt - -# bulk-add from the guids.txt with a single `rsconnect content build add` command -xargs printf -- '-g %s\n' < guids.txt | xargs rsconnect content build add -``` -## Programmatic Provisioning - -Posit Connect supports the programmatic bootstrapping of an administrator API key -for scripted provisioning tasks. This process is supported by the `rsconnect bootstrap` command, -which uses a JSON Web Token to request an initial API key from a fresh Connect instance. - -```bash -rsconnect bootstrap \ - --server https://connect.example.org:3939 \ - --jwt-keypath /path/to/secret.key -``` - -A full description on how to use `rsconnect bootstrap` in a provisioning workflow is provided in the Connect administrator guide's -[programmatic provisioning](https://docs.posit.co/connect/admin/programmatic-provisioning) documentation. - -## Server Administration Tasks - -Starting with the 2023.05 edition of Posit Connect, `rsconnect-python` can be -used to perform certain server administration tasks, such as instance managing -runtime caches. For more information on runtime caches in Posit Connect, see the -Connect Admin Guide's section on [runtime -caches](https://docs.posit.co/connect/admin/server-management/runtime-caches/). - -Examples in this section will use `--name myserver` to stand in for your Connect -server information. See [Managing Server -Information](#managing-server-information) above for more details. - -### Enumerate Runtime Caches -*New in Connect 2023.05* - -Use the command below to enumerate runtime caches on a Connect server. The -command will output a JSON object containing a list of runtime caches . Each -cache entry will contain the following information: - -- `language`: The language of content that uses the cache, either R or Python. -- `version`: The language version of the content that uses the cache. -- `image_name`: The execution environment of the cache. The string `Local` - denotes native execution. For Connect instances that use off-host execution, - the name of the image that uses the cache will be displayed. - -```bash -rsconnect system caches list --name myserver -# { -# "caches": [ -# { -# "language": "R", -# "version": "3.6.3", -# "image_name": "Local" -# }, -# { -# "language": "Python", -# "version": "3.9.5", -# "image_name": "Local" -# }, -# { -# "language": "R", -# "version": "3.6.3", -# "image_name": "rstudio/content-base:r3.6.3-py3.9.5-bionic" -# }, -# { -# "language": "Python", -# "version": "3.9.5", -# "image_name": "rstudio/content-base:r3.6.3-py3.9.5-bionic" -# } -# ] -# } -``` - -> **Note** -> The `image_name` field returned by the server will use sanitized versions -> of names. - -### Delete Runtime Caches -*New in Connect 2023.05* - -When Connect's execution environment changes, runtime caches may be invalidated. -In these cases, you will need to delete the affected runtime caches using the -`system caches delete` command. - -> **Warning** -> After deleting a cache, the first time affected content is visited, Connect -> will need to reconstruct its environment. This can take a long time. To -> mitigate this, you can use the [`content build`](#content-build) command to -> rebuild affected content ahead of time. You may want to do this just for -> high-priority content, or for all content. - -To delete a runtime cache, call the `system caches delete` command, specifying a -Connect server, as well as the language (`-l, --language`), version (`-V, ---version`), and image name (`-I, --image-name`) for the cache you wish to -delete. Deleting a large cache might take a while. The command will wait for -Connect to finish the task. - -Use the following parameters specify the target cache: - -- `language` (required) must name `R` or `Python`. It is case-insensitive. -- `version` (required) must be a three-part version number, e.g. `3.8.12`. -- `image-name` (optional) defaults to `Local`, which targets caches used for - natively-executed content. Off-host images can be specified using either the - literal image name or the sanitized name returned by the `list` command. - -Use the dry run flag (`-d, --dry-run`) to surface any errors ahead of -deletion. - -```bash -rsconnect system caches delete \ - --name myserver \ - --language Python \ - --version 3.9.5 \ - --image-name rstudio/content-base:r3.6.3-py3.9.5-bionic \ - --dry-run -# Dry run finished - -rsconnect system caches delete \ - --name myserver \ - --language Python \ - --version 3.9.5 \ - --image-name rstudio/content-base:r3.6.3-py3.9.5-bionic -# Deleting runtime cache... -# Successfully deleted runtime cache -``` - -You should run these commands for each cache you wish to delete. diff --git a/docs/programmatic-provisioning.md b/docs/programmatic-provisioning.md new file mode 100644 index 00000000..fec6d747 --- /dev/null +++ b/docs/programmatic-provisioning.md @@ -0,0 +1,14 @@ +# Programmatic Provisioning + +Posit Connect supports the programmatic bootstrapping of an administrator API key +for scripted provisioning tasks. This process is supported by the `rsconnect bootstrap` command, +which uses a JSON Web Token to request an initial API key from a fresh Connect instance. + +```bash +rsconnect bootstrap \ + --server https://connect.example.org:3939 \ + --jwt-keypath /path/to/secret.key +``` + +A full description on how to use `rsconnect bootstrap` in a provisioning workflow is provided in the Connect administrator guide's +[programmatic provisioning](https://docs.posit.co/connect/admin/programmatic-provisioning) documentation. diff --git a/docs/server-administration.md b/docs/server-administration.md new file mode 100644 index 00000000..857ac83b --- /dev/null +++ b/docs/server-administration.md @@ -0,0 +1,517 @@ +# Server Administration + +Starting with the 2023.05 edition of Posit Connect, `rsconnect-python` can be +used to perform certain server administration tasks, such as instance managing +runtime caches. For more information on runtime caches in Posit Connect, see the +Connect Admin Guide's section on [runtime +caches](https://docs.posit.co/connect/admin/server-management/runtime-caches/). + +Examples in this section will use `--name myserver` to stand in for your Connect +server information. See [Managing Server +Information](#managing-server-information) above for more details. + +## Runtime Caches + +### Enumerate Runtime Caches + +*New in Connect 2023.05* + +Use the command below to enumerate runtime caches on a Connect server. The +command will output a JSON object containing a list of runtime caches . Each +cache entry will contain the following information: + +- `language`: The language of content that uses the cache, either R or Python. +- `version`: The language version of the content that uses the cache. +- `image_name`: The execution environment of the cache. The string `Local` + denotes native execution. For Connect instances that use off-host execution, + the name of the image that uses the cache will be displayed. + +```bash +rsconnect system caches list --name myserver +# { +# "caches": [ +# { +# "language": "R", +# "version": "3.6.3", +# "image_name": "Local" +# }, +# { +# "language": "Python", +# "version": "3.9.5", +# "image_name": "Local" +# }, +# { +# "language": "R", +# "version": "3.6.3", +# "image_name": "rstudio/content-base:r3.6.3-py3.9.5-bionic" +# }, +# { +# "language": "Python", +# "version": "3.9.5", +# "image_name": "rstudio/content-base:r3.6.3-py3.9.5-bionic" +# } +# ] +# } +``` + +> **Note** +> The `image_name` field returned by the server will use sanitized versions +> of names. + +### Delete Runtime Caches + +*New in Connect 2023.05* + +When Connect's execution environment changes, runtime caches may be invalidated. +In these cases, you will need to delete the affected runtime caches using the +`system caches delete` command. + +> **Warning** +> After deleting a cache, the first time affected content is visited, Connect +> will need to reconstruct its environment. This can take a long time. To +> mitigate this, you can use the [`content build`](#content-build) command to +> rebuild affected content ahead of time. You may want to do this just for +> high-priority content, or for all content. + +To delete a runtime cache, call the `system caches delete` command, specifying a +Connect server, as well as the language (`-l, --language`), version (`-V, +--version`), and image name (`-I, --image-name`) for the cache you wish to +delete. Deleting a large cache might take a while. The command will wait for +Connect to finish the task. + +Use the following parameters specify the target cache: + +- `language` (required) must name `R` or `Python`. It is case-insensitive. +- `version` (required) must be a three-part version number, e.g. `3.8.12`. +- `image-name` (optional) defaults to `Local`, which targets caches used for + natively-executed content. Off-host images can be specified using either the + literal image name or the sanitized name returned by the `list` command. + +Use the dry run flag (`-d, --dry-run`) to surface any errors ahead of +deletion. + +```bash +rsconnect system caches delete \ + --name myserver \ + --language Python \ + --version 3.9.5 \ + --image-name rstudio/content-base:r3.6.3-py3.9.5-bionic \ + --dry-run +# Dry run finished + +rsconnect system caches delete \ + --name myserver \ + --language Python \ + --version 3.9.5 \ + --image-name rstudio/content-base:r3.6.3-py3.9.5-bionic +# Deleting runtime cache... +# Successfully deleted runtime cache +``` + +You should run these commands for each cache you wish to delete. + +## Content subcommands + +rsconnect-python supports multiple options for interacting with Posit Connect's +`/v1/content` API. Both administrators and publishers can use the content subcommands +to search, download, and rebuild content on Posit Connect without needing to access the +dashboard from a browser. + +> **Note** +> The `rsconnect content` CLI subcommands are intended to be easily scriptable. +> The default output format is `JSON` so that the results can be easily piped into +> other command line utilities like [`jq`](https://stedolan.github.io/jq/) for further post-processing. + +```bash +rsconnect content --help +# Usage: rsconnect content [OPTIONS] COMMAND [ARGS]... + +# Interact with Posit Connect's content API. + +# Options: +# --help Show this message and exit. + +# Commands: +# build Build content on Posit Connect. +# describe Describe a content item on Posit Connect. +# download-bundle Download a content item's source bundle. +# search Search for content on Posit Connect. +``` + +### Content Search + +The `rsconnect content search` subcommands can be used by administrators and publishers +to find specific content on a given Posit Connect server. The search returns +metadata for each content item that meets the search criteria. + +```bash +rsconnect content search --help +# Usage: rsconnect content search [OPTIONS] + +# Options: +# -n, --name TEXT The nickname of the Posit Connect server. +# -s, --server TEXT The URL for the Posit Connect server. +# -k, --api-key TEXT The API key to use to authenticate with +# Posit Connect. + +# -i, --insecure Disable TLS certification/host validation. +# -c, --cacert FILENAME The path to trusted TLS CA certificates. +# --published Search only published content. +# --unpublished Search only unpublished content. +# --content-type [unknown|shiny|rmd-static|rmd-shiny|static|api|tensorflow-saved-model|jupyter-static|python-api|python-dash|python-streamlit|python-bokeh|python-fastapi|python-gradio|quarto-shiny|quarto-static] +# Filter content results by content type. +# --r-version VERSIONSEARCHFILTER +# Filter content results by R version. +# --py-version VERSIONSEARCHFILTER +# Filter content results by Python version. +# --title-contains TEXT Filter content results by title. +# --order-by [created|last_deployed] +# Order content results. +# -v, --verbose Print detailed messages. +# --help Show this message and exit. + +rsconnect content search +# [ +# { +# "max_conns_per_process": null, +# "content_category": "", +# "load_factor": null, +# "cluster_name": "Local", +# "description": "", +# "bundle_id": "142", +# "image_name": null, +# "r_version": null, +# "content_url": "https://connect.example.org:3939/content/4ffc819c-065c-420c-88eb-332db1133317/", +# "connection_timeout": null, +# "min_processes": null, +# "last_deployed_time": "2021-12-02T18:09:11Z", +# "name": "logs-api-python", +# "title": "logs-api-python", +# "created_time": "2021-07-19T19:17:32Z", +# "read_timeout": null, +# "guid": "4ffc819c-065c-420c-88eb-332db1133317", +# "parameterized": false, +# "run_as": null, +# "py_version": "3.8.2", +# "idle_timeout": null, +# "app_role": "owner", +# "access_type": "acl", +# "app_mode": "python-api", +# "init_timeout": null, +# "id": "18", +# "quarto_version": null, +# "dashboard_url": "https://connect.example.org:3939/connect/#/apps/4ffc819c-065c-420c-88eb-332db1133317", +# "run_as_current_user": false, +# "owner_guid": "edf26318-0027-4d9d-bbbb-54703ebb1855", +# "max_processes": null +# }, +# ... +# ] +``` + +See [this section](#searching-for-content) for more comprehensive usage examples +of the available search flags. + + +### Content Build + +> **Note** +> The `rsconnect content build` subcommand requires Posit Connect >= 2021.11.1 + +Posit Connect caches R and Python packages in the configured +[`Server.DataDir`](https://docs.posit.co/connect/admin/appendix/configuration/#Server.DataDir). +Under certain circumstances (examples below), these package caches can become stale +and need to be rebuilt. This refresh automatically occurs when a Posit Connect +user visits the content. You may wish to refresh some content before it is visited +because it is high priority or is not visited frequently (API content, emailed reports). +In these cases, it is possible to preemptively build specific content items using +the `rsconnect content build` subcommands. This way the user does not have to pay +the build cost when the content is accessed next. + +The following are some common scenarios where performing a content build might be necessary: + +- OS upgrade +- changes to gcc or libc libraries +- changes to Python or R installations +- switching from source to binary package repositories or vice versa + +> **Note** +> The `content build` command is non-destructive, meaning that it does nothing to purge +> existing packrat/python package caches before a build. If you have an +> existing cache, it should be cleared prior to starting a content build. +> See the [migration documentation](https://docs.posit.co/connect/admin/appendix/cli/#migration) for details. + +> **Note** +> You may use the [`rsconnect content search`](#content-search) subcommand to help +> identify high priority content items to build. + +```bash +rsconnect content build --help +Usage: rsconnect content build [OPTIONS] COMMAND [ARGS]... + + Build content on Posit Connect. Requires Connect >= 2021.11.1 + +Options: + --help Show this message and exit. + +Commands: + add Mark a content item for build. Use `build run` to invoke the build + on the Connect server. + + history Get the build history for a content item. + logs Print the logs for a content build. + ls List the content items that are being tracked for build on a given + Connect server. + + rm Remove a content item from the list of content that are tracked for + build. Use `build ls` to view the tracked content. + + run Start building content on a given Connect server. +``` + +To build a specific content item, first `add` it to the list of content that is +"tracked" for building using its GUID. Content that is "tracked" in the local state +may become out-of-sync with what exists remotely on the Connect server (the result of +`rsconnect content search`). When this happens, it is safe to remove the locally tracked +entries with `rsconnect content build rm`. + +> **Note** +> Metadata for "tracked" content items is stored in a local directory called +> `rsconnect-build` which will be automatically created in your current working directory. +> You may set the environment variable `CONNECT_CONTENT_BUILD_DIR` to override this directory location. + +```bash +# `add` the content to mark it as "tracked" +rsconnect content build add --guid 4ffc819c-065c-420c-88eb-332db1133317 + +# run the build which kicks off a cache rebuild on the server +rsconnect content build run + +# once the build is complete, the content can be "untracked" +# this does not remove the content from the Connect server +# the entry is only removed from the local state file +rsconnect content build rm --guid 4ffc819c-065c-420c-88eb-332db1133317 +``` + +> **Note** +> See [this section](#add-to-build-from-search-results) for +> an example of how to add multiple content items in bulk, from the results +> of a `rsconnect content search` command. + +To view all currently "tracked" content items, use the `rsconnect content build ls` subcommand. + +```bash +rsconnect content build ls +``` + +To view only the "tracked" content items that have not yet been built, use the `--status NEEDS_BUILD` flag. + +```bash +rsconnect content build ls --status NEEDS_BUILD +``` + +Once the content items have been added, you may initiate a build +using the `rsconnect content build run` subcommand. This command will attempt to +build all "tracked" content that has the status `NEEDS_BUILD`. + +> To re-run failed builds, use `rsconnect content build run --retry`. This will build +all tracked content in any of the following states: `[NEEDS_BUILD, ABORTED, ERROR, RUNNING]`. +> +> If you encounter an error indicating that a build operation is already in progress, +you can use `rsconnect content build run --force` to bypass the check and proceed with building content marked as `NEEDS_BUILD`. +Ensure no other build operation is actively running before using the `--force` option. + +```bash +rsconnect content build run +# [INFO] 2021-12-14T13:02:45-0500 Initializing ContentBuildStore for https://connect.example.org:3939 +# [INFO] 2021-12-14T13:02:45-0500 Starting content build (https://connect.example.org:3939)... +# [INFO] 2021-12-14T13:02:45-0500 Starting build: 4ffc819c-065c-420c-88eb-332db1133317 +# [INFO] 2021-12-14T13:02:50-0500 Running = 1, Pending = 0, Success = 0, Error = 0 +# [INFO] 2021-12-14T13:02:50-0500 Build succeeded: 4ffc819c-065c-420c-88eb-332db1133317 +# [INFO] 2021-12-14T13:02:55-0500 Running = 0, Pending = 0, Success = 1, Error = 0 +# [INFO] 2021-12-14T13:02:55-0500 1/1 content builds completed in 0:00:10 +# [INFO] 2021-12-14T13:02:55-0500 Success = 1, Error = 0 +# [INFO] 2021-12-14T13:02:55-0500 Content build complete. +``` + +Sometimes content builds will fail and require debugging by the publisher or administrator. +Use the `rsconnect content build ls` to identify content builds that resulted in errors +and inspect the build logs with the `rsconnect content build logs` subcommand. + +```bash +rsconnect content build ls --status ERROR +# [INFO] 2021-12-14T13:07:32-0500 Initializing ContentBuildStore for https://connect.example.org:3939 +# [ +# { +# "rsconnect_build_status": "ERROR", +# "last_deployed_time": "2021-12-02T18:09:11Z", +# "owner_guid": "edf26318-0027-4d9d-bbbb-54703ebb1855", +# "rsconnect_last_build_log": "/Users/david/code/posit/rsconnect-python/rsconnect-build/logs/connect_example_org_3939/4ffc819c-065c-420c-88eb-332db1133317/pZoqfBoi6BgpKde5.log", +# "guid": "4ffc819c-065c-420c-88eb-332db1133317", +# "rsconnect_build_task_result": { +# "user_id": 1, +# "error": "Cannot find compatible environment: no compatible Local environment with Python version 3.9.5", +# "code": 1, +# "finished": true, +# "result": { +# "data": "An error occurred while building the content", +# "type": "build-failed-error" +# }, +# "id": "pZoqfBoi6BgpKde5" +# }, +# "dashboard_url": "https://connect.example.org:3939/connect/#/apps/4ffc819c-065c-420c-88eb-332db1133317", +# "name": "logs-api-python", +# "title": "logs-api-python", +# "content_url": "https://connect.example.org:3939/content/4ffc819c-065c-420c-88eb-332db1133317/", +# "bundle_id": "141", +# "rsconnect_last_build_time": "2021-12-14T18:07:16Z", +# "created_time": "2021-07-19T19:17:32Z", +# "app_mode": "python-api" +# } +# ] + +rsconnect content build logs --guid 4ffc819c-065c-420c-88eb-332db1133317 +# [INFO] 2021-12-14T13:09:27-0500 Initializing ContentBuildStore for https://connect.example.org:3939 +# Building Python API... +# Cannot find compatible environment: no compatible Local environment with Python version 3.9.5 +# Task failed. Task exited with status 1. +``` + +Once a build for a piece of tracked content is complete, it can be safely removed from the list of "tracked" +content by using `rsconnect content build rm` command. This command accepts a `--guid` argument to specify +which piece of content to remove. Removing the content from the list of tracked content simply removes the item +from the local state file, the content deployed to the server remains unchanged. + +```bash +rsconnect content build rm --guid 4ffc819c-065c-420c-88eb-332db1133317 +``` + +### Rebuilding lots of content + +When attempting to rebuild a long list of content, it is recommended to first build a sub-set of the content list. +First choose 1 or 2 Python and R content items for each version of Python and R on the server. Try to choose content +items that have the most dependencies in common with other content items on the server. Build these content items +first with the `rsconnect content build run` command. This will "warm" the Python and R environment cache for subsequent +content builds. Once these initial builds are complete, add the remaining content items to the list of "tracked" content +and execute another `rsconnect content build run` command. + +To execute multiple content builds simultaniously, use the `rsconnect content build run --parallelism` flag to increase the +number of concurrent builds. By default, each content item is built serially. Increasing the build parallelism can reduce the total +time needed to rebuild a long list of content items. We recommend starting with a low parallelism setting (2-3) and increasing +from there to avoid overloading the Connect server with concurrent build operations. Remember that these builds are executing on the +Connect server which consumes CPU, RAM, and i/o bandwidth that would otherwise we allocated for Python and R applications +running on the server. + +### Usage Examples + +#### Searching for content + +The following are some examples of how publishers might use the +`rsconnect content search` subcommand to find content on Posit Connect. +By default, the `rsconnect content search` command will return metadata for ALL +of the content on a Posit Connect server, both published and unpublished content. + +> **Note** +> When using the `--r-version` and `--py-version` flags, users should +> make sure to quote the arguments to avoid conflicting with your shell. For +> example, bash would interpret `--py-version >3.0.0` as a shell redirect because of the +> unquoted `>` character. + +```bash +# return only published content +rsconnect content search --published + +# return only unpublished content +rsconnect content search --unpublished + +# return published content where the python version is at least 3.9.0 +rsconnect content search --published --py-version ">=3.9.0" + +# return published content where the R version is exactly 3.6.3 +rsconnect content search --published --r-version "==3.6.3" + +# return published content where the content type is a static RMD +rsconnect content search --content-type rmd-static + +# return published content where the content type is either shiny OR fast-api +rsconnect content search --content-type shiny --content-type python-fastapi + +# return all content, published or unpublished, where the title contains the +# text "Stock Report" +rsconnect content search --title-contains "Stock Report" + +# return published content, results are ordered by when the content was last +# deployed +rsconnect content search --published --order-by last_deployed + +# return published content, results are ordered by when the content was +# created +rsconnect content search --published --order-by created +``` + +#### Finding R and Python versions + +One common use for the `search` command might be to find the versions of +R and python that are currently in use on your Posit Connect server before a migration. + +```bash +# search for all published content and print the unique r and python version +# combinations +rsconnect content search --published | jq -c '.[] | {py_version,r_version}' | sort | +uniq +# {"py_version":"3.8.2","r_version":"3.5.3"} +# {"py_version":"3.8.2","r_version":"3.6.3"} +# {"py_version":"3.8.2","r_version":null} +# {"py_version":null,"r_version":"3.5.3"} +# {"py_version":null,"r_version":"3.6.3"} +# {"py_version":null,"r_version":null} +``` + +#### Finding recently deployed content + +```bash +# return only the 10 most recently deployed content items +rsconnect content search \ + --order-by last_deployed \ + --published | jq -c 'limit(10; .[]) | { guid, last_deployed_time }' +# {"guid":"4ffc819c-065c-420c-88eb-332db1133317","last_deployed_time":"2021-12-02T18:09:11Z"} +# {"guid":"aa2603f8-1988-484f-a335-193f2c57e6c4","last_deployed_time":"2021-12-01T20:56:07Z"} +# {"guid":"051252f0-4f70-438f-9be1-d818a3b5f8d9","last_deployed_time":"2021-12-01T20:37:01Z"} +# {"guid":"015143da-b75f-407c-81b1-99c4a724341e","last_deployed_time":"2021-11-30T16:56:21Z"} +# {"guid":"bcc74209-3a81-4b9c-acd5-d24a597c256c","last_deployed_time":"2021-11-30T15:51:07Z"} +# {"guid":"f21d7767-c99e-4dd4-9b00-ff8ec9ae2f53","last_deployed_time":"2021-11-23T18:46:28Z"} +# {"guid":"da4f709c-c383-4fbc-89e2-f032b2d7e91d","last_deployed_time":"2021-11-23T18:46:28Z"} +# {"guid":"9180809d-38fd-4730-a0e0-8568c45d87b7","last_deployed_time":"2021-11-23T15:16:19Z"} +# {"guid":"2b1d2ab8-927d-4956-bbf9-29798d039bc5","last_deployed_time":"2021-11-22T18:33:17Z"} +# {"guid":"c96db3f3-87a1-4df5-9f58-eb109c397718","last_deployed_time":"2021-11-19T20:25:33Z"} +``` + +#### Add to build from search results + +One common use case might be to `rsconnect content build add` content for build +based on the results of a `rsconnect content search`. For example: + +```bash +# search for all API type content, then +# for each guid, add it to the "tracked" content items +for guid in $(rsconnect content search \ + --published \ + --content-type python-api \ + --content-type api | jq -r '.[].guid'); do + rsconnect content build add --guid $guid +done +``` + +Adding content items one at a time can be a slow operation. This is because +`rsconnect content build add` must fetch metadata for each content item before it +is added to the "tracked" content items. By providing multiple `--guid` arguments +to the `rsconnect content build add` subcommand, we can fetch metadata for multiple content items +in a single api call, which speeds up the operation significantly. + +```bash +# write the guid of every published content item to a file called guids.txt +rsconnect content search --published | jq '.[].guid' > guids.txt + +# bulk-add from the guids.txt with a single `rsconnect content build add` command +xargs printf -- '-g %s\n' < guids.txt | xargs rsconnect content build add +``` diff --git a/mkdocs.yml b/mkdocs.yml index a6a1c4e9..96dd18fd 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -1,4 +1,4 @@ -site_name: 'Posit Connect: rsconnect-python' +site_name: 'rsconnect-python' copyright: Posit Software, PBC. All Rights Reserved markdown_extensions: @@ -33,7 +33,10 @@ plugins: - search nav: - - Home: index.md + - Getting Started: index.md + - Programmatic Provisioning: programmatic-provisioning.md + - Deploying Content: deploying.md + - Server Administration: server-administration.md - CLI reference: - rsconnect: - add: commands/add.md From 35bf93312cf7b38bf507c8ad3e3a43b4fa48088d Mon Sep 17 00:00:00 2001 From: edavidaja Date: Wed, 7 May 2025 13:24:28 -0400 Subject: [PATCH 4/4] update reqs --- docs/requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/requirements.txt b/docs/requirements.txt index fd39a2e5..8d5e6d93 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -6,3 +6,4 @@ mkdocs-exclude mkdocs-macros-plugin mkdocs-material pymdown-extensions +mkdocs-click