# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/papagorgio23/Python101/blob/master/Connect_to_GBQ.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="raSQtIn4grDa" colab_type="text"
# # Connect to GBQ

# + [markdown] id="YmAcVKnXg3JA" colab_type="text"
# ## Install Library

# + id="8UVMPEFCgiZn" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 360} outputId="bb6e092d-a993-42c0-ade2-dbfe7ff35ba4"
# Installing Library
# !pip install pydata_google_auth

# + [markdown] id="BBlKswCCg6tK" colab_type="text"
# ## Get Credentials

# + id="ayRZaPmtgsk1" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 71} outputId="32793162-9b33-467f-dd5d-68bedcdfd7b8"
# Using GBQ shout Out to Hughes
import pandas_gbq
import pydata_google_auth

SCOPES = [
    'https://www.googleapis.com/auth/cloud-platform',
    'https://www.googleapis.com/auth/drive',
]

credentials = pydata_google_auth.get_user_credentials(
    SCOPES,
    auth_local_webserver=False)

# + [markdown] id="PbgBZafNhLSD" colab_type="text"
# ## Write SQL Script

# + id="YnWAhx7-g_n9" colab_type="code" colab={}
sql = """

SELECT *
FROM `ffn-dw-bigquery-prd.Credit_Views.Check_Sales_NPV_Model_Inputs`
LIMIT 1000

"""

# + [markdown] id="4PwrtrBahbjx" colab_type="text"
# ## Execute Query

# + [markdown] id="ZyqsG1Kyhzfk" colab_type="text"
# The first query it will make you authenticate one more time. After that you can run as many queries as you need and you won't have to authenticate during the session.

# + id="faY6txClhayx" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 71} outputId="efffddf6-996c-4985-b46c-8ff849ff4f5e"
df = pandas_gbq.read_gbq(sql, project_id='ffn-dw-bigquery-prd', credentials=credentials, dialect='standard')

# + id="xuxfcdyQhnIv" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 309} outputId="97294b8a-48a0-4bba-8544-50e636aa06a4"
df.head()

# + id="rkDClebhhyCp" colab_type="code" colab={}


StarcoderdataJupyterScriptsDedupFiltered