Repository: reflex-dev/reflex
Pull request: Empty .web/pages directory on recompile
Empty the .web/pages directory on recompile so that old pages that don't exist anymore in the python code get removed.

Problem statement:
Old pages aren't deleted on recompile
**Describe the bug**
Old pages stay in the web directory

**To Reproduce**
Steps to reproduce the behavior:
* compile the app with pages ["page1", "page2"]
* compile the app with just ["page1"]
* the file `/.web/page2` still exists

**Expected behavior**
Only the specified pages should exist - old ones should be deleted.

**Screenshots**
If applicable, add screenshots to help explain your problem.

** Specifics (please complete the following information):**
 - Python Version:
 - Pynecone Version:
 - OS: 
 - Browser (Optional):
 
**Additional context**
Add any other context about the problem here.

Hints from issue discussion:

Commit message: added ignored files 404.js and _app.js when .web/pages is purged

Patch to review:
diff --git a/pynecone/app.py b/pynecone/app.py
index d6a7c4f74bf..2df8b13bc26 100644
--- a/pynecone/app.py
+++ b/pynecone/app.py
@@ -284,6 +284,9 @@ def compile(self, force_compile: bool = False):
         # Create the database models.
         Model.create_all()
 
+        #Empty the .web pages directory
+        compiler.purge_web_pages_dir()
+
         # Compile the root document with base styles and fonts.
         compiler.compile_document_root(self.stylesheets)
 
diff --git a/pynecone/compiler/compiler.py b/pynecone/compiler/compiler.py
index 4b96e4dfbef..91c51d1c34f 100644
--- a/pynecone/compiler/compiler.py
+++ b/pynecone/compiler/compiler.py
@@ -208,3 +208,10 @@ def compile_components(components: Set[CustomComponent]):
     # Compile the components.
     code = _compile_components(components)
     return output_path, code
+
+
+def purge_web_pages_dir():
+    """Empty out .web directory.
+    """
+    template_files = ["_app.js","404.js"]
+    utils.empty_dir(constants.WEB_PAGES_DIR)
\ No newline at end of file
diff --git a/pynecone/compiler/utils.py b/pynecone/compiler/utils.py
index 3a2e84f0243..9ed27f122ba 100644
--- a/pynecone/compiler/utils.py
+++ b/pynecone/compiler/utils.py
@@ -304,3 +304,17 @@ def write_page(path: str, code: str):
     utils.mkdir(os.path.dirname(path))
     with open(path, "w") as f:
         f.write(code)
+
+def empty_dir(path, ignored=[]):
+    """Remove all files and folders in a directory except for the ignored file- or foldernames.
+
+    Args:
+        path (str): The path to the directory that will be emptied
+        excluded (list, optional): List of filenames or foldernames that will not be deleted. Defaults to [].
+    """
+    directory_contents = os.listdir(path)
+    print(directory_contents)
+    for element in directory_contents:
+        if element not in ignored:
+            utils.rm(os.path.join(path, element))
+    
\ No newline at end of file


Code review comments:
diff_hunk: @@ -208,3 +208,10 @@ def compile_components(components: Set[CustomComponent]):
     # Compile the components.
     code = _compile_components(components)
     return output_path, code
+
+
+def purge_web_pages_dir():
+    """Empty out .web directory.
+    """
+    template_files = ["_app.js","404.js"]
path: pynecone/compiler/compiler.py
text: @user1:
I think you forgot to pass this in as an argument

diff_hunk: @@ -304,3 +304,17 @@ def write_page(path: str, code: str):
     utils.mkdir(os.path.dirname(path))
     with open(path, "w") as f:
         f.write(code)
+
+def empty_dir(path, ignored=[]):
+    """Remove all files and folders in a directory except for the ignored file- or foldernames.
+
+    Args:
+        path (str): The path to the directory that will be emptied
+        excluded (list, optional): List of filenames or foldernames that will not be deleted. Defaults to [].
+    """
+    directory_contents = os.listdir(path)
+    print(directory_contents)
path: pynecone/compiler/utils.py
text: @user1:
We can remove this print