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
- Download the HTML: Save the HTML page (e.g., “Loans Business - AppSheet.html”) that contains your app’s relationships.
- Prepare Your Folder: Copy the downloaded HTML file and the Python script (shown below) into the same folder on your computer.
- Install Python: Download and install Python from python.org if you don’t have it installed already.
- Install BeautifulSoup: Open your command prompt (or terminal) and run: pip install beautifulsoup4
- 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.