# Writing Python Code

## Do you want to write Python code?

1. First, let's create a new project in the BlackDiamond Studio [web portal](https://bds.mobilize.net/) by clicking the **+** button.

![](https://3318121499-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FltqydcnGV3BWCCC3QkYO%2Fuploads%2F4koWshS5G98htR0aGLMR%2Fno-project.jpg?alt=media\&token=ca9c2b33-5f56-4abf-b3b7-5d4d466d51f3)

2\. Select the **Code from templates** option. With this project type, you can use several code templates to facilitate your work. Templates are a set of source code files and pre-installed dependencies that help you start coding faster and easier.&#x20;

<figure><img src="https://3318121499-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FltqydcnGV3BWCCC3QkYO%2Fuploads%2FlPYlVmG4C2DjIabn6kRU%2FScreen%20Shot%202022-11-02%20at%2017.11.53.png?alt=media&#x26;token=518fdf1f-3da9-4571-8fc3-0203ed1c69ed" alt=""><figcaption></figcaption></figure>

3\. Name your project.&#x20;

<figure><img src="https://3318121499-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FltqydcnGV3BWCCC3QkYO%2Fuploads%2FI8zKWsVI6J6G4rtNXr7B%2FScreen%20Shot%202022-11-02%20at%2017.21.39.png?alt=media&#x26;token=79def6f6-407f-40fd-ad8b-cd0ca4a8c1d7" alt=""><figcaption></figcaption></figure>

4\. [Set up the project repository](https://docs.gapvelocity.ai/blackdiamond-studio-or-mobilize.net/blackdiamond-studio-documentation/bds-portal/creating-my-first-project/3.-set-up-repository). You can create a new repository in BlackDiamond Studio powered by GitLab or connect your existing GitHub repository.

<figure><img src="https://3318121499-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FltqydcnGV3BWCCC3QkYO%2Fuploads%2FvzFjmlwOmsjZrUMs6F3S%2FScreen%20Shot%202022-11-02%20at%2017.23.33.png?alt=media&#x26;token=758ed96a-9a9a-4ec2-bf29-7b478c976373" alt=""><figcaption></figcaption></figure>

5\. As an optional step, [connect to your Snowflake account](https://docs.gapvelocity.ai/blackdiamond-studio-or-mobilize.net/blackdiamond-studio-documentation/bds-portal/creating-my-first-project/4.-connect-to-snowflake). This is required to deploy to Snowflake.

<figure><img src="https://3318121499-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FltqydcnGV3BWCCC3QkYO%2Fuploads%2FqNBzXtCkuvzvW3Xcpzm0%2FScreen%20Shot%202022-11-02%20at%2017.41.17.png?alt=media&#x26;token=912e930e-902b-4ae2-8140-83d6190155a7" alt=""><figcaption></figcaption></figure>

6\. Select the **Python language** and select the template of your choice. This will populate your repository with Python code that you can use to deploy to Snowflake with the Snowpark API.

<figure><img src="https://3318121499-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FltqydcnGV3BWCCC3QkYO%2Fuploads%2F1CUXYNAtGWpKfEsmVFCo%2FPython.png?alt=media&#x26;token=9c1ab66c-4c96-4050-b5ec-dbaf369ae3d4" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3318121499-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FltqydcnGV3BWCCC3QkYO%2Fuploads%2FYqhvZce0AaNq5VXAwwWC%2FScreen%20Shot%202022-11-04%20at%2010.37.59.png?alt=media&#x26;token=e87569d8-0147-4b68-bbcd-03ae8369f4ea" alt=""><figcaption></figcaption></figure>

7\. Click the **Finish setup** button.

8\. Click the **Open in IDE** button on the project page.

![](https://3318121499-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FltqydcnGV3BWCCC3QkYO%2Fuploads%2Fg5BntwgQt7orw3knrOgm%2FScreen%20Shot%202022-06-14%20at%2012.38.33.png?alt=media\&token=9e7d9d8e-7b8b-4111-81be-3851595fb8ba)

## Now that you are in the BlackDiamond Studio IDE, what do you want to do?

### 1. I want to use Snowpark with Python

#### Getting started

1. In your project repositor&#x79;**,** you will find the **main.py** file that contains a basic example of how to create a Snowpark session, which then can be used to interact with the Snowflake account you should be connected to.
2. A Snowflake connection is required to use Snowpark with Python. This connection can be set up when you create the project in the [web portal](https://bds.mobilize.net/), or it can be managed in the BlackDiamond Studio IDE with the SQL Tools extension.
3. The SQL Tools extension can be accessed by clicking on the cylinder icon on the Activity Bar (on the left side of the IDE).

#### **Creating and exporting a Stored Procedure / User Defined Function (UDF)**

1. Snowflake stored procedures and UDFs can be written in Python and then exported to your Snowflake account.&#x20;
2. Stored procedures and UDFs can be called or can be used during queries after being exported. They can be invoked from your SQL code or even using Snowpark.&#x20;
3. If you want to create and export a Stored Procedure / UDF, follow these steps:&#x20;
   1. Make sure you have a Python file (with .py extension).&#x20;
   2. Right-click on the Python file and then click on **Black Diamond: Export Python UDF**. This will create a template file called **UDF-Export-Snowflake-Python.sql**, located in the **target** folder.\
      &#x20;![](https://3318121499-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FltqydcnGV3BWCCC3QkYO%2Fuploads%2FrLss23WJWi22Bp922tde%2FMicrosoftTeams-image%20\(70\).png?alt=media\&token=a1e245da-8f7c-4534-b764-35000894b5e0)\
      ![](https://3318121499-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FltqydcnGV3BWCCC3QkYO%2Fuploads%2FknBuNIMRX0VR0F9oPh5W%2FMicrosoftTeams-image%20\(71\).png?alt=media\&token=65405a5e-6024-4386-978a-929bc98c4105)
   3. Replace the placeholders in the **UDF-Export-Snowflake-Python.sql** template file with the actual values that you want to use. If you are creating and exporting a UDF, you can ignore or remove section 3.1, since it is used for creating a Stored Procedure. Likewise, if you are exporting a Stored Procedure, you can ignore or remove section 3.2, since it is used for creating a UDF.
   4. After filling the placeholders, you can run the SQL commands. Left-click on the **UDF-Export-Snowflake-Python.sql** and then click on **Black Diamond: Deploy**. This will execute the SQL script and create the UDF/Procedure.&#x20;
   5. You can reorganize the template and change it in order to create multiple functions/procedures with the same script.

### 2. I just want to use Python, I do not need Snowpark  &#x20;

#### Getting started

* An example of how to write a "Hello World!" program on Python is given at the beginning of the **main.py** file.
* You can code just like you would typically with Python.
* You can remove the Snowpark code if you don't need it (this includes the **user\_defined\_functions.py** and the **procedures.py** files.

## **Recommendations**

### Deploying UDFs/Procedures&#x20;

* Make sure the packages you want to import are installed in the Snowflake account you are connected to. This query can help you verify the packages that are already installed in the account:&#x20;

```sql
select * from information_schema.packages where language = 'python';
```

* The procedures must receive a session as the first parameter. This parameter should not be included in the CREATE PROCEDURE statement.&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.gapvelocity.ai/blackdiamond-studio-or-mobilize.net/blackdiamond-studio-documentation/online-code-editor/writing-python-code.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
