mirror of https://github.com/databricks/cli.git
Format default-python template (#2110)
## Changes Format code in default-python template, so it's already pre-formatted. ## Tests ``` $ databricks bundle init libs/template/templates/default-python $ ruff format --diff my_project 6 files already formatted ```
This commit is contained in:
parent
55494a0bda
commit
25f8ee8d66
|
@ -29,7 +29,8 @@
|
||||||
"source": [
|
"source": [
|
||||||
{{- if (eq .include_python "yes") }}
|
{{- if (eq .include_python "yes") }}
|
||||||
"import sys\n",
|
"import sys\n",
|
||||||
"sys.path.append('../src')\n",
|
"\n",
|
||||||
|
"sys.path.append(\"../src\")\n",
|
||||||
"from {{.project_name}} import main\n",
|
"from {{.project_name}} import main\n",
|
||||||
"\n",
|
"\n",
|
||||||
"main.get_taxis(spark).show(10)"
|
"main.get_taxis(spark).show(10)"
|
||||||
|
|
|
@ -5,28 +5,32 @@ This file is primarily used by the setuptools library and typically should not
|
||||||
be executed directly. See README.md for how to deploy, test, and run
|
be executed directly. See README.md for how to deploy, test, and run
|
||||||
the {{.project_name}} project.
|
the {{.project_name}} project.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from setuptools import setup, find_packages
|
from setuptools import setup, find_packages
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
sys.path.append('./src')
|
|
||||||
|
sys.path.append("./src")
|
||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import {{.project_name}}
|
import {{.project_name}}
|
||||||
|
|
||||||
|
local_version = datetime.datetime.utcnow().strftime("%Y%m%d.%H%M%S")
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name="{{.project_name}}",
|
name="{{.project_name}}",
|
||||||
# We use timestamp as Local version identifier (https://peps.python.org/pep-0440/#local-version-identifiers.)
|
# We use timestamp as Local version identifier (https://peps.python.org/pep-0440/#local-version-identifiers.)
|
||||||
# to ensure that changes to wheel package are picked up when used on all-purpose clusters
|
# to ensure that changes to wheel package are picked up when used on all-purpose clusters
|
||||||
version={{.project_name}}.__version__ + "+" + datetime.datetime.utcnow().strftime("%Y%m%d.%H%M%S"),
|
version={{.project_name}}.__version__ + "+" + local_version,
|
||||||
url="https://databricks.com",
|
url="https://databricks.com",
|
||||||
author="{{user_name}}",
|
author="{{user_name}}",
|
||||||
description="wheel file based on {{.project_name}}/src",
|
description="wheel file based on {{.project_name}}/src",
|
||||||
packages=find_packages(where='./src'),
|
packages=find_packages(where="./src"),
|
||||||
package_dir={'': 'src'},
|
package_dir={"": "src"},
|
||||||
entry_points={
|
entry_points={
|
||||||
"packages": [
|
"packages": [
|
||||||
"main={{.project_name}}.main:main"
|
"main={{.project_name}}.main:main",
|
||||||
]
|
],
|
||||||
},
|
},
|
||||||
install_requires=[
|
install_requires=[
|
||||||
# Dependencies in case the output wheel file is used as a library dependency.
|
# Dependencies in case the output wheel file is used as a library dependency.
|
||||||
|
|
|
@ -35,6 +35,7 @@
|
||||||
"# Import DLT and src/{{.project_name}}\n",
|
"# Import DLT and src/{{.project_name}}\n",
|
||||||
"import dlt\n",
|
"import dlt\n",
|
||||||
"import sys\n",
|
"import sys\n",
|
||||||
|
"\n",
|
||||||
"sys.path.append(spark.conf.get(\"bundle.sourcePath\", \".\"))\n",
|
"sys.path.append(spark.conf.get(\"bundle.sourcePath\", \".\"))\n",
|
||||||
"from pyspark.sql.functions import expr\n",
|
"from pyspark.sql.functions import expr\n",
|
||||||
"from {{.project_name}} import main"
|
"from {{.project_name}} import main"
|
||||||
|
@ -63,17 +64,18 @@
|
||||||
{{- if (eq .include_python "yes") }}
|
{{- if (eq .include_python "yes") }}
|
||||||
"@dlt.view\n",
|
"@dlt.view\n",
|
||||||
"def taxi_raw():\n",
|
"def taxi_raw():\n",
|
||||||
" return main.get_taxis(spark)\n",
|
" return main.get_taxis(spark)\n",
|
||||||
{{else}}
|
{{else}}
|
||||||
"\n",
|
"\n",
|
||||||
"@dlt.view\n",
|
"@dlt.view\n",
|
||||||
"def taxi_raw():\n",
|
"def taxi_raw():\n",
|
||||||
" return spark.read.format(\"json\").load(\"/databricks-datasets/nyctaxi/sample/json/\")\n",
|
" return spark.read.format(\"json\").load(\"/databricks-datasets/nyctaxi/sample/json/\")\n",
|
||||||
{{end -}}
|
{{end -}}
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
"@dlt.table\n",
|
"@dlt.table\n",
|
||||||
"def filtered_taxis():\n",
|
"def filtered_taxis():\n",
|
||||||
" return dlt.read(\"taxi_raw\").filter(expr(\"fare_amount < 30\"))"
|
" return dlt.read(\"taxi_raw\").filter(expr(\"fare_amount < 30\"))"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
|
|
@ -1,21 +1,25 @@
|
||||||
from pyspark.sql import SparkSession, DataFrame
|
from pyspark.sql import SparkSession, DataFrame
|
||||||
|
|
||||||
|
|
||||||
def get_taxis(spark: SparkSession) -> DataFrame:
|
def get_taxis(spark: SparkSession) -> DataFrame:
|
||||||
return spark.read.table("samples.nyctaxi.trips")
|
return spark.read.table("samples.nyctaxi.trips")
|
||||||
|
|
||||||
|
|
||||||
# Create a new Databricks Connect session. If this fails,
|
# Create a new Databricks Connect session. If this fails,
|
||||||
# check that you have configured Databricks Connect correctly.
|
# check that you have configured Databricks Connect correctly.
|
||||||
# See https://docs.databricks.com/dev-tools/databricks-connect.html.
|
# See https://docs.databricks.com/dev-tools/databricks-connect.html.
|
||||||
def get_spark() -> SparkSession:
|
def get_spark() -> SparkSession:
|
||||||
try:
|
try:
|
||||||
from databricks.connect import DatabricksSession
|
from databricks.connect import DatabricksSession
|
||||||
return DatabricksSession.builder.getOrCreate()
|
|
||||||
except ImportError:
|
return DatabricksSession.builder.getOrCreate()
|
||||||
return SparkSession.builder.getOrCreate()
|
except ImportError:
|
||||||
|
return SparkSession.builder.getOrCreate()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
get_taxis(get_spark()).show(5)
|
get_taxis(get_spark()).show(5)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in New Issue