Python script to export data relationships from your AppSheet app

I recently needed to inventory the relationships in my app to plan a migration of my data source from Google Sheets to an AppSheet Database. Unfortunately, the option to copy these relationships directly from data relationships settings wasn’t available.

To solve this, I created a Python script that scrapes the HTML page containing the relationships and outputs them in a plain text file (one relationship per line). Below you can see the Python script, along with simple step-by-step instructions for those who may not be familiar with coding or Python.

Step-by-Step Instructions

  1. Download the HTML: Save the HTML page (e.g., “Loans Business - AppSheet.html”) that contains your app’s relationships.
  2. Prepare Your Folder: Copy the downloaded HTML file and the Python script (shown below) into the same folder on your computer.
  3. Install Python: Download and install Python from python.org if you don’t have it installed already.
  4. Install BeautifulSoup: Open your command prompt (or terminal) and run: pip install beautifulsoup4
  5. Run the Script: In your command prompt, navigate to the folder containing the files and run the script with: python your_script_name.py

Python Script

# Python script to extract relationships from an HTML file using BeautifulSoup
# Requirements: pip install beautifulsoup4

from bs4 import BeautifulSoup

def extract_relationships(html_file, output_file):
    # Open and read the HTML file content
    with open(html_file, 'r', encoding='utf-8') as f:
        content = f.read()
    
    # Parse the HTML with BeautifulSoup
    soup = BeautifulSoup(content, 'html.parser')
    
    # Select the div elements that contain the relationships
    # Using a CSS selector that matches divs with the three classes:
    # "SketchFact", "SketchFact-Enabled", and "SketchFact-UserMade"
    elements = soup.select("div.SketchFact.SketchFact-Enabled.SketchFact-UserMade")
    
    relationships = []
    for elem in elements:
        # Extract the inner text using a separator to preserve spaces and trim extra whitespace
        text = elem.get_text(separator=" ", strip=True)
        relationships.append(text)
    
    # Write each relationship to a new line in the output file
    with open(output_file, 'w', encoding='utf-8') as f:
        for relationship in relationships:
            f.write(relationship + "\n")
    
    print(f"{len(relationships)} relationships extracted and saved in '{output_file}'.")

if __name__ == "__main__":
    html_file = "My app - AppSheet.html"  # Name of the downloaded HTML file
    output_file = "extracted_relationships.txt"    # File where the relationships will be saved
    extract_relationships(html_file, output_file)
  

I hope this tip helps anyone facing a similar challenge during their migration process.

6 Likes

Hi @verasand ,

Thank you so much for sharing this incredibly useful Python script! Your solution came at the perfect time for my project.

I was working on creating technical documentation for our AppSheet app, specifically trying to generate ER diagrams from our app structure. Your HTML scraping approach was exactly what I needed and proved to be much more reliable than trying to parse PDF documentation pages.

Following your script, I was able to:

  • Successfully extract 35 relationships and analyze 17 tables from our app
  • Generate a comprehensive Mermaid ER diagram automatically
  • Save countless hours of manual documentation work

Your contribution to the community is invaluable - this kind of practical, working solution is exactly what makes AppSheet development more efficient for everyone. Thank you for taking the time to document the step-by-step instructions so clearly!

3 Likes

This is so impressive

The use of BeautifulSoup is a clever way to parse the haystack

Why did I not read this comment a couple of months ago when I migrated from AppSheet Databases to a SQLServer and manually moved all the table relationships! Certainly will take this into account next time.

2 Likes