Retired_mod
Esteemed Contributor III

Hi @ToReSa, If you just want to execute the notebook, calling another notebook would be easier. You can even exchange some data between the notebooks.

But if you specifically want to pick each SQL from one notebook and execute it in another notebook, you should go with this approach:-

  • Export notebook A as JSON: Export the notebook to JSON for programmatic access.

  • Extract SQL Commands: Read the JSON file in notebook B and extract SQL commands from each cell.

  • Execute SQL Commands: Run each SQL command in notebook B and save results to CSV files.

Here is the sample script:-

import json
import requests
import pandas as pd

# Export notebookA as JSON
response = requests.get(
    'https://your-databricks-instance/api/2.0/workspace/export',
    headers={'Authorization': 'Bearer your_databricks_token'},
    params={'path': '/path/to/notebookA', 'format': 'SOURCE'}
)
notebookA_content = response.json()

# Extract SQL commands
cells = notebookA_content['content']['cells']
sql_commands = [cell['source'] for cell in cells if cell['cell_type'] == 'code']

# Execute and export results
for i, sql in enumerate(sql_commands):
    result = spark.sql(sql)
    result_df = result.toPandas()
    result_df.to_csv(f'result_{i}.csv', index=False)

Please review the responses and let us know which best addresses your question. Your feedback is valuable to us and the community.

If the response resolves your issue, kindly mark it as the accepted solution. This will help close the thread and assist others with similar queries.

We appreciate your participation and are here if you need further assistance!