### Sample Usage Help Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/container/snippets/README.md This example demonstrates how to get usage and argument information for a Python sample script. ```bash # example python quickstart.py -h usage: quickstart.py [-h] project_id zone positional arguments: project_id Google Cloud project ID zone GKE Cluster zone optional arguments: -h, --help show this help message and exit ``` -------------------------------- ### Run the Application Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/cloud-sql/sql-server/sqlalchemy/README.md Start the Python demo application locally after setting up the environment and installing dependencies. Access the application via http://127.0.0.1:8080. ```bash python app.py ``` -------------------------------- ### Install Dependencies Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/people-and-planet-ai/weather-forecasting/notebooks/1-overview.ipynb Upgrades setuptools and installs the local 'weather-data' package required for the sample. ```shell # Upgrade `setuptools` to install packages from pyproject.toml files. !pip install --quiet --upgrade --no-warn-conflicts pip setuptools # Install the `weather-data` local package. !pip install serving/weather-data ``` -------------------------------- ### Setup Demo Application Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/trace/cloud-trace-demo-app-opentelemetry/README.md Executes the setup script to deploy the demo application services using a pre-built container image. ```bash ./setup.sh ``` -------------------------------- ### Install Libraries and Clone Repository Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/people-and-planet-ai/image-classification/README.ipynb Installs necessary libraries like libffi-dev, clones the python-docs-samples repository, navigates to the sample directory, and installs project requirements. ```python # We need libffi-dev to launch the Dataflow pipeline. !apt-get -qq install libffi-dev # Clone the python-docs-samples respository. !git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git # Navigate to the sample code directory. %cd python-docs-samples/people-and-planet-ai/image-classification # Install the sample requirements. !pip install --quiet -U pip !pip install -r requirements.txt ``` -------------------------------- ### Run Compute Library Quickstart Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/compute/client_library/snippets/README.md Executes the quickstart script to demonstrate creating and destroying a Compute Engine instance. This requires prior authentication with gcloud. ```bash python quickstart.py ``` -------------------------------- ### Install Requirements Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/cloud-sql/mysql/client-side-encryption/README.md Sets up a virtual environment and installs necessary Python packages from requirements.txt. ```bash virtualenv --python python3 env source env/bin/activate pip install -r requirements.txt ``` -------------------------------- ### Install sample requirements Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/dataflow/encryption-keys/README.md Installs the Python dependencies required for the encryption-keys sample. ```sh pip install -U -r requirements.txt ``` -------------------------------- ### Install sample dependencies Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/composer/rest/README.rst Install the necessary Python packages for the samples using pip. Ensure you have a requirements.txt file in your sample directory. ```bash $ pip install -r requirements.txt ``` -------------------------------- ### Start Static Service Locally Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/appengine/flexible/multiple_services/README.md Use this command to set up the virtual environment and start the static file service locally. Ensure you are in the 'static-service' directory. ```Bash cd static-service virtualenv -p python3 env source env/bin/activate pip install -r requirements.txt python main.py ``` -------------------------------- ### Start Gateway Service Locally Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/appengine/flexible/multiple_services/README.md Use this command to set up the virtual environment and start the gateway service locally. Ensure you are in the 'gateway-service' directory. ```Bash cd gateway-service virtualenv -p python3 env source env/bin/activate pip install -r requirements.txt python main.py ``` -------------------------------- ### Example Folder Structure for 'Hello World' Feature Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/genai/template_folder/README.md An example demonstrating the folder structure for a 'Hello World' feature that takes username input. Note the naming conventions for the folder, sample file, and test file. ```text genai/ └── hello_world/ ├── noxfile_config.py ├── requirements-test.txt ├── requirements.txt ├── helloworld_with_txt.py └── test_hello_world_examples.py ``` -------------------------------- ### Install Dependencies for Sentiment Analysis Sample Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/language/snippets/sentiment/README.md Create a virtual environment, activate it, and install the required Python packages using the requirements.txt file. ```bash virtualenv env source env/bin/activate (env)$ pip install -r requirements.txt ``` -------------------------------- ### Install Sample Dependencies Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/aml-ai/README.md Install the necessary Python dependencies for the samples using pip and the requirements.txt file. Ensure your virtual environment is activated before running this command. ```bash pip install -r requirements.txt ``` -------------------------------- ### Install Dependencies Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/people-and-planet-ai/geospatial-classification/README.ipynb Installs the required Python packages for the project from the requirements.txt file. Warnings during installation can typically be ignored for this sample. ```python !pip install -r requirements.txt -c constraints.txt ``` -------------------------------- ### Install pgvector and other Python libraries Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/cloud-sql/postgres/pgvector/notebooks/pgvector_gen_ai_demo.ipynb Install the pgvector Python client and other necessary libraries for the demo. This is a prerequisite for interacting with the database. ```python pip install --upgrade google-cloud-sql-connector pgvector langchain-community langchain-google-genai ``` -------------------------------- ### Install Dependencies using virtualenv Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/endpoints/bookstore-grpc-transcoding/README.md Set up a virtual environment and install Python dependencies from requirements.txt. ```bash virtualenv bookstore-env source bookstore-env/bin/activate pip install -r requirements.txt ``` -------------------------------- ### Copy Example Secrets File Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/auth/custom-credentials/okta/README.md Copies the example secrets file to a new file for local configuration. ```bash cp custom-credentials-okta-secrets.json.example custom-credentials-okta-secrets.json ``` -------------------------------- ### Install Dependencies using Pip Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/notebooks/README.md Install project dependencies by upgrading and installing from the requirements.txt file. This is necessary for running the project locally. ```bash pip install --upgrade -r requirements.txt ``` -------------------------------- ### Start Python Application Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/appengine/flexible/datastore/README.md Execute this command to start the Python application locally. Ensure you have the necessary dependencies and configurations set up. ```bash python main.py ``` -------------------------------- ### Install Google Cloud Retail Libraries Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/retail/interactive-tutorials/README.md Installs necessary Google Cloud client libraries for Python, including Retail, Storage, and BigQuery. ```bash pip install google pip install google-cloud-retail pip install google-cloud-storage pip install google-cloud-bigquery ``` -------------------------------- ### Install Dependencies for IAP Samples Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/iap/README.md Installs the necessary Python libraries for running the IAP samples. Ensure you are in the correct working directory and have a virtual environment set up. ```bash virtualenv/bin/pip install -r requirements.txt ``` -------------------------------- ### Install Dependencies and Run Local App Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/appengine/flexible/README.md Installs project dependencies using pip and runs the main application script for local testing. ```bash pip install -r requirements.txt python main.py ``` -------------------------------- ### Run Datastore Tasks Example App Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/datastore/cloud-client/README.rst Execute the tasks example application to demonstrate Datastore API operations. This sample likely involves creating, reading, updating, and deleting tasks. ```bash python tasks.py ``` -------------------------------- ### Installing Own Dependencies After Airflow Setup Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/composer/workflows/constraints.txt After a reproducible Airflow installation, use this command to install your own dependencies. Constraints are not used here, and 'apache-airflow==X.Y.Z' is specified to prevent accidental upgrades or downgrades of the Airflow version. ```bash pip install "apache-airflow==X.Y.Z" "snowflake-connector-python[pandas]=N.M.O" ``` -------------------------------- ### Create a Complete Training Example Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/people-and-planet-ai/land-cover-classification/README.ipynb Combines input and label patches to form a single training example. Ensures consistency by using the same functions for input and label data retrieval. ```python def get_training_example( lonlat: tuple[float, float], patch_size: int = 128 ) -> tuple[np.ndarray, np.ndarray]: return ( get_input_patch(2020, lonlat, patch_size), get_label_patch(lonlat, patch_size), ) point = (-110.312, 30.972) # (lon, lat) (inputs, labels) = get_training_example(point) print(f"inputs : {inputs.dtype} {inputs.shape}") print(f"labels : {labels.dtype} {labels.shape}") ``` -------------------------------- ### Run Hybrid Tutorial Sample Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/translate/samples/snippets/hybrid_glossaries/README.rst Execute the hybrid tutorial sample that demonstrates using glossaries with Google Cloud Vision and Text-to-Speech APIs. ```bash python hybrid_tutorial.py ``` -------------------------------- ### Example RAG question answering Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/cloud-sql/postgres/pgvector/notebooks/pgvector_gen_ai_demo.ipynb Demonstrate the RAG system by asking a question and getting an answer generated based on the retrieved context from the pgvector database. ```python print(answer_question("What is the capital of France?")) ``` -------------------------------- ### DICOMweb Search Studies Example Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/healthcare/api-client/v1/dicom/README.rst Handles GET requests to search for DICOM studies according to the DICOMweb standard. Requires specifying search parameters. ```bash python dicomweb.py dicomweb-search-studies --project_id PROJECT_ID --location LOCATION --dataset_id DATASET_ID --dicom_store_id DICOM_STORE_ID ``` -------------------------------- ### DICOMweb Retrieve Study Example Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/healthcare/api-client/v1/dicom/README.rst Handles GET requests to retrieve a DICOM study according to the DICOMweb standard. Requires specifying the study UID. ```bash python dicomweb.py dicomweb-retrieve-study --project_id PROJECT_ID --location LOCATION --dataset_id DATASET_ID --dicom_store_id DICOM_STORE_ID --study_uid STUDY_UID ``` -------------------------------- ### Write Training Examples to NumPy File Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/people-and-planet-ai/weather-forecasting/notebooks/2-dataset.ipynb Writes a batch of training examples (inputs and labels) into a compressed NumPy file (.npz) in the specified data path. Ensure `create_dataset.py` is available. ```python from create_dataset import write_npz data_path = "data/" batch = [(inputs, labels)] write_npz(batch, data_path) ``` -------------------------------- ### Visualize Training Example Data Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/people-and-planet-ai/land-cover-classification/README.ipynb Displays a single training example, showing the input image bands and the corresponding land cover labels. Requires a `visualize` module with a `show_example` function. ```python from visualize import show_example show_example(inputs, labels) ``` -------------------------------- ### DICOMweb Retrieve Instance Example Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/healthcare/api-client/v1/dicom/README.rst Handles GET requests to retrieve a specific DICOM instance according to the DICOMweb standard. Requires specifying study and instance UIDs. ```bash python dicomweb.py dicomweb-retrieve-instance --project_id PROJECT_ID --location LOCATION --dataset_id DATASET_ID --dicom_store_id DICOM_STORE_ID --study_uid STUDY_UID --instance_uid INSTANCE_UID ``` -------------------------------- ### DICOMweb Retrieve Rendered Example Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/healthcare/api-client/v1/dicom/README.rst Handles GET requests to retrieve a rendered version of a DICOM instance according to the DICOMweb standard. Requires specifying study and instance UIDs. ```bash python dicomweb.py dicomweb-retrieve-rendered --project_id PROJECT_ID --location LOCATION --dataset_id DATASET_ID --dicom_store_id DICOM_STORE_ID --study_uid STUDY_UID --series_uid SERIES_UID --instance_uid INSTANCE_UID ``` -------------------------------- ### DICOMweb Search Instance Example Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/healthcare/api-client/v1/dicom/README.rst Handles GET requests to search for DICOM instances according to the DICOMweb standard. Requires specifying search parameters like study UID. ```bash python dicomweb.py dicomweb-search-instance --project_id PROJECT_ID --location LOCATION --dataset_id DATASET_ID --dicom_store_id DICOM_STORE_ID --study_uid STUDY_UID ``` -------------------------------- ### Fetch and Print Training Example Data Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/people-and-planet-ai/weather-forecasting/notebooks/2-dataset.ipynb Fetches a training example using `get_training_example` and prints its data types and shapes. Handles transient errors with `Retry`. Ensure `create_dataset.py` is available. ```python from datetime import datetime from create_dataset import get_training_example date = datetime(2019, 9, 2, 18) point = [-77.93, 25.23] # [longitude, latitude] (inputs, labels) = get_training_example(date, point, patch_size=64) print(f"inputs : {inputs.dtype} {inputs.shape}") print(f"labels : {labels.dtype} {labels.shape}") ``` -------------------------------- ### DICOMweb Delete Study Example Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/healthcare/api-client/v1/dicom/README.rst Handles DELETE requests to delete a DICOM study according to the DICOMweb standard, equivalent to WADO-RS GET requests. Requires specifying the study UID. ```bash python dicomweb.py dicomweb-delete-study --project_id PROJECT_ID --location LOCATION --dataset_id DATASET_ID --dicom_store_id DICOM_STORE_ID --study_uid STUDY_UID ``` -------------------------------- ### Run Wiki Sample Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/blog/introduction_to_data_models_in_cloud_datastore/README.md Executes the wiki sample, requiring your Google Cloud project ID. ```bash python wiki.py your-project-id ``` -------------------------------- ### Install PortAudio on macOS using Homebrew Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/scripts/readme-gen/templates/install_portaudio.tmpl.rst Use this command to install PortAudio on macOS if you have Homebrew installed. This is a prerequisite for installing PyAudio. ```bash brew install portaudio ``` -------------------------------- ### Installing Custom Dependencies After Airflow Installation Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/composer/2022_airflow_summit/constraints.txt After a reproducible Airflow installation, use this command to install your own dependencies. This command does not use constraints and pins the Airflow version to prevent accidental upgrades or downgrades. ```bash pip install "apache-airflow==X.Y.Z" "snowflake-connector-python[pandas]==2.9.0" ``` -------------------------------- ### Run Crop Hints Sample Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/vision/snippets/crop_hints/README.rst Execute the crop_hints.py sample script. This script requires an image file path and a mode ('crop' or 'draw') as arguments. ```bash python crop_hints.py ``` -------------------------------- ### Run Sample Locally Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/appengine/standard/README.md Starts a local development server for an App Engine application. ```bash dev_appserver.py app.yaml ``` -------------------------------- ### Install Apache Beam Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/people-and-planet-ai/land-cover-classification/land-cover-change.ipynb Installs the Apache Beam library, which is used for data processing pipelines. The runtime is restarted after installation. ```python !pip install --quiet apache-beam # Restart the runtime by ending the process. exit() ``` -------------------------------- ### Start Django Project Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/appengine/flexible/hello_world_django/README.md Initializes a new Django project with the specified project name. ```bash django-admin startproject project_name ``` -------------------------------- ### Install pyenv using Homebrew Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/MAC_SETUP.md Installs the pyenv version manager using Homebrew. Ensure Homebrew is up-to-date before installation. ```bash brew update brew install pyenv ``` -------------------------------- ### Run Blog Sample Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/blog/introduction_to_data_models_in_cloud_datastore/README.md Executes the blog sample, requiring your Google Cloud project ID. ```bash python blog.py your-project-id ``` -------------------------------- ### Navigate to Sample Directory Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/README.md Change the current directory to a specific sample folder, such as logging/cloud-client. ```bash cd logging/cloud-client/ ``` -------------------------------- ### Install Dependencies Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/people-and-planet-ai/weather-forecasting/notebooks/2-dataset.ipynb Installs necessary Python packages, including Apache Beam with GCP support and the local 'weather-data' package, after upgrading setuptools and installing build tools. ```python # Upgrade `setuptools` to install packages from pyproject.toml files. !pip install --quiet --upgrade --no-warn-conflicts pip setuptools # We need `build` and `virtualenv` to build the local packages. !pip install --quiet build virtualenv # Install Apache Beam and the `weather-data` local package. !pip install apache-beam[gcp] serving/weather-data ``` -------------------------------- ### Install Python Packages Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/cloud-sql/postgres/pgvector/notebooks/pgvector_gen_ai_demo.ipynb Installs essential Python packages including asyncio, asyncpg, cloud-sql-python-connector, numpy, pandas, pgvector, langchain, transformers, and google-cloud-aiplatform. Restarting the kernel is required after installation. ```python # Install dependencies. !pip install asyncio==3.4.3 asyncpg==0.27.0 cloud-sql-python-connector["asyncpg"]==1.2.3 !pip install numpy==1.22.4 pandas==1.5.3 !pip install pgvector==0.1.8 !pip install langchain==0.0.196 transformers==4.30.1 !pip install google-cloud-aiplatform==1.26.0 ``` ```python # Automatically restart kernel after installs so that your environment # can access the new packages. import IPython app = IPython.Application.instance() app.kernel.do_shutdown(True) ``` -------------------------------- ### Install Dependencies for Local Packages Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/people-and-planet-ai/weather-forecasting/notebooks/4-predictions.ipynb Installs necessary packages like `setuptools`, `build`, and `virtualenv` to enable the installation of local Python packages. This step is crucial for setting up the environment for predictions. ```bash # Upgrade `setuptools` to install packages from pyproject.toml files. !pip install --quiet --upgrade --no-warn-conflicts pip setuptools # We need `build` and `virtualenv` to build the local packages. !pip install --quiet build virtualenv ``` -------------------------------- ### Navigate to Retail Interactive Tutorials Directory Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/retail/interactive-tutorials/README.md Changes the current directory to the root of the Retail interactive tutorials samples. ```bash cd python-docs-samples/retail/interactive-tutorials ``` -------------------------------- ### Navigate to Sample Directory Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/pubsub/streaming-analytics/README.md Change the current directory to the Pub/Sub streaming analytics sample. ```bash cd python-docs-samples/pubsub/streaming-analytics ``` -------------------------------- ### Install pgvector extension Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/cloud-sql/postgres/pgvector/notebooks/pgvector_gen_ai_demo.ipynb Install the pgvector extension on your Cloud SQL for PostgreSQL instance. ```shell gcloud sql instances patch INSTANCE_NAME --database-flags "cloudsql.enable_pglogical=on" ``` -------------------------------- ### Run Product Creation Script Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/retail/interactive-tutorials/README.md Executes a Python script to set up a Cloud Storage bucket and upload product data. ```bash python product/setup_product/products_create_gcs_bucket.py ``` -------------------------------- ### Create GCE Instance with Startup Script Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/error_reporting/fluent_on_compute/README.md Use this command to create a Google Compute Engine instance with a provided startup script. The script configures Fluentd for error reporting. ```bash gcloud compute instances create example-instance --metadata-from-file startup-script=startup_script.sh ``` -------------------------------- ### Install BigQuery DataFrames Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/bigquery/bigframes/README.md Install or upgrade the BigQuery DataFrames library using pip. ```bash pip install --upgrade bigframes ``` -------------------------------- ### Install pyenv-virtualenv plugin Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/MAC_SETUP.md Installs the pyenv-virtualenv plugin to manage virtual environments with pyenv. ```bash brew install pyenv-virtualenv ``` -------------------------------- ### Run Python Sample Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/scripts/readme-gen/templates/README.tmpl.rst This command executes a Python sample. Use the --help flag for more options if available. ```bash python {{sample.file}} ``` -------------------------------- ### Install and Upgrade Datascience Package Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/people-and-planet-ai/timeseries-classification/README.ipynb Installs or upgrades the 'datascience' package to the latest version quietly. ```python # Make sure we have the latest version of `datascience` for visualization. %pip install --quiet --upgrade datascience ``` -------------------------------- ### Create Service Account and Key Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/pubsub/streaming-analytics/README.md Create a service account, assign it the Project Owner role, and generate a JSON key file for authentication. ```sh export PROJECT_ID=$(gcloud config get-value project) export SERVICE_ACCOUNT_NAME=samples export IAM_ACCOUNT=$SERVICE_ACCOUNT_NAME@$PROJECT_ID.iam.gserviceaccount.com # Create the service account. gcloud iam service-accounts create $SERVICE_ACCOUNT_NAME \ --display-name $SERVICE_ACCOUNT_NAME # Set the role to Project Owner (*). gcloud projects add-iam-policy-binding $PROJECT_ID \ --member serviceAccount:$IAM_ACCOUNT \ --role roles/owner # Create a JSON file with the service account credentials. gcloud iam service-accounts keys create path/to/your/credentials.json \ --iam-account=$IAM_ACCOUNT ``` -------------------------------- ### Navigate to sample directory Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/dataflow/encryption-keys/README.md Changes the current directory to the encryption-keys sample directory within the cloned repository. ```sh cd python-docs-samples/dataflow/encryption-keys ``` -------------------------------- ### Reproducible Airflow Installation with Providers Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/composer/workflows/constraints.txt Use this command to install a specific version of Airflow along with selected providers, ensuring reproducibility by referencing the constraints file. This is the primary method for installing Airflow with dependencies that are known to be compatible. ```bash pip install "apache-airflow[celery,cncf.kubernetes,google,amazon,snowflake]==X.Y.Z" \ --constraint \ "https://raw.githubusercontent.com/apache/airflow/constraints-X.Y.Z/constraints-3.11.txt" ``` -------------------------------- ### Navigate to Sample Directory After Restart Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/people-and-planet-ai/weather-forecasting/notebooks/2-dataset.ipynb Navigates back into the sample directory after the runtime has been restarted. ```python %cd python-docs-samples/people-and-planet-ai/weather-forecasting ``` -------------------------------- ### Install Coreutils on MacOS Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md On MacOS, install coreutils using Homebrew if needed for local test scripts. ```shell brew install coreutils ``` -------------------------------- ### Build Sample Container Locally Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/run/README.md Build a Docker container for a Cloud Run sample locally. Ensure you have Docker installed and navigate to the sample's directory. ```bash export SAMPLE= cd $SAMPLE docker build --tag $SAMPLE . ``` -------------------------------- ### Install a specific Python version Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/MAC_SETUP.md Installs a specific version of Python (e.g., 3.6.13) using pyenv. ```bash pyenv install 3.6.13 ``` -------------------------------- ### Build Docker Image Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/run/image-processing/README.md Builds the Docker image for the sample service. Ensure you have Docker installed and running. ```bash docker build --tag pubsub-tutorial:python . ``` -------------------------------- ### Clone Repository and Navigate Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/people-and-planet-ai/weather-forecasting/notebooks/4-predictions.ipynb Clones the GitHub repository containing the sample code and navigates into the specific project directory. This is a prerequisite for running the subsequent code. ```bash # Now let's get the code from GitHub and navigate to the sample. !git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git %cd python-docs-samples/people-and-planet-ai/weather-forecasting ``` -------------------------------- ### Install Dependencies Locally Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/appengine/standard/README.md Installs project dependencies into the 'lib' directory for local development using pip. ```bash pip install -t lib -r requirements.txt ``` -------------------------------- ### Setup Cloud SQL Instance and PostgreSQL Database Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/cloud-sql/postgres/pgvector/notebooks/pgvector_gen_ai_demo.ipynb Creates a Cloud SQL PostgreSQL instance if one does not exist, creates the specified database if it doesn't exist, and creates the database user with the provided password. ```bash #@markdown Create and setup a Cloud SQL PostgreSQL instance, if not done already. database_version = !gcloud sql instances describe {instance_name} --format="value(databaseVersion)" if database_version[0].startswith("POSTGRES"): print("Found an existing Postgres Cloud SQL Instance!") else: print("Creating new Cloud SQL instance...") !gcloud sql instances create {instance_name} --database-version=POSTGRES_15 \ --region={region} --cpu=1 --memory=4GB --root-password={database_password} # Create the database, if it does not exist. out = !gcloud sql databases list --instance={instance_name} --filter="NAME:{database_name}" --format="value(NAME)" if ''.join(out) == database_name: print("Database %s already exists, skipping creation." % database_name) else: !gcloud sql databases create {database_name} --instance={instance_name} # Create the database user for accessing the database. !gcloud sql users create {database_user} \ --instance={instance_name} \ --password={database_password} ``` -------------------------------- ### Install dependencies Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/people-and-planet-ai/land-cover-classification/cloud-tensorflow.ipynb Installs the project dependencies listed in requirements.txt. This command requires a runtime restart to take effect. ```python # Install the dependencies. !pip install --quiet -r requirements.txt # Restart the runtime by ending the process. exit() ``` -------------------------------- ### Initialize Google Cloud SDK Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/appengine/flexible/datastore/README.md Run this command to initialize the Google Cloud SDK, which is necessary for local authentication when using Google Cloud APIs. ```bash gcloud init ``` -------------------------------- ### Install Nox Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/AUTHORING_GUIDE.md Installs the nox package globally using pip. This is a prerequisite for running automated tests with nox. ```sh pip install nox ``` -------------------------------- ### Running Python Samples Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/container/snippets/README.md This snippet shows the general command to run Python samples and how to access help for a specific sample. ```python python ... ``` ```python python -h ``` -------------------------------- ### Install Django and Gunicorn Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/appengine/flexible/hello_world_django/README.md Installs the Django web framework and Gunicorn, a WSGI HTTP Server, using pip. ```bash pip install django gunicorn ``` -------------------------------- ### Install Compute Library Requirements Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/compute/client_library/snippets/README.md Installs the necessary libraries for the google-cloud-compute library. Ensure you have a virtual environment set up and activated. ```bash virtualenv --python python3 name-of-your-virtualenv source name-of-your-virtualenv/bin/activate pip install -r ../requirements.txt ``` -------------------------------- ### Install Python Virtual Environment Package Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/cloud-media-livestream/keypublisher/README.md Install the `python3-venv` package using `apt-get` to manage Python virtual environments. ```shell sudo apt-get install python3-venv ``` -------------------------------- ### Deploy Sample App Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/appengine/standard/migration/ndb/redis_cache/README.md Deploy the sample application using gcloud command. Ensure `app3.yaml` and `index.yaml` are configured. ```bash gcloud app deploy app3.yaml index.yaml ``` -------------------------------- ### Build Docker Image Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/run/hello-broken/README.md Builds the Docker image for the hello-broken sample. ```docker docker build --tag hello-broken:python . ``` -------------------------------- ### Test Sample with Nox Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/run/hello-broken/README.md Runs tests for the sample using nox. Ensure nox is installed via 'pip install nox'. ```shell nox -s "py36(sample='./run/hello-broken')" ``` -------------------------------- ### Install Dependencies Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/endpoints/getting-started-grpc/README.md Installs project dependencies using virtualenv and pip. Ensure you have Python 3 and the Google Cloud SDK configured. ```bash virtualenv -p python3 env source env/bin/activate pip install -r requirements.txt ``` -------------------------------- ### Deploy to Cloud Run Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/run/pubsub/README.md Deploy the Pub/Sub tutorial service to Cloud Run. This involves submitting a build to Google Cloud Build and then deploying the image to Cloud Run. ```bash # Set an environment variable with your GCP Project ID export GOOGLE_CLOUD_PROJECT= # Submit a build using Google Cloud Build gcloud builds submit --tag gcr.io/${GOOGLE_CLOUD_PROJECT}/pubsub-tutorial # Deploy to Cloud Run gcloud run deploy pubsub-tutorial --image gcr.io/${GOOGLE_CLOUD_PROJECT}/pubsub-tutorial ``` -------------------------------- ### Setup Google Cloud Environment Variables Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/cloud-sql/postgres/pgvector/notebooks/pgvector_gen_ai_demo.ipynb Sets up Google Cloud environment variables including project ID, database password, region, instance name, database name, and user. Includes basic input validation. ```python # @markdown Replace the required placeholder text below. You can modify any other default values, if you like. # Please fill in these values. project_id = "[YOUR_PROJECT_ID **REQUIRED**]" # @param {type:"string"} database_password = "[YOUR_PASSWORD **REQUIRED**]" # @param {type:"string"} region = "us-west2" # @param {type:"string"} instance_name = "pg15-pgvector-demo" # @param {type:"string"} database_name = "retail" # @param {type:"string"} database_user = "retail-admin" # @param {type:"string"} # Quick input validations. assert project_id, "⚠️ Please provide a Google Cloud project ID" assert region, "⚠️ Please provide a Google Cloud region" assert instance_name, "⚠️ Please provide the name of your instance" assert database_name, "⚠️ Please provide a database name" assert database_user, "⚠️ Please provide a database user" assert database_password, "⚠️ Please provide a database password" ``` -------------------------------- ### Install pgvector extension Source: https://github.com/googlecloudplatform/python-docs-samples/blob/main/cloud-sql/postgres/pgvector/notebooks/pgvector_gen_ai_demo.ipynb This command installs the pgvector extension in your Cloud SQL for PostgreSQL instance. Ensure you have the necessary permissions and that the instance is running. ```bash gcloud sql instances patch "$(gcloud sql instances list --filter="databaseVersion=POSTGRES_15" --format='value(name)')" --database-flags=cloudsql.enable_extensions=pgvector ```