@@ -150,27 +150,27 @@ def _populate_demo_app(name_variants: NameVariants):
150150 from reflex .compiler import templates
151151 from reflex .reflex import _init
152152
153- demo_app_dir = name_variants .demo_app_dir
153+ demo_app_dir = Path ( name_variants .demo_app_dir )
154154 demo_app_name = name_variants .demo_app_name
155155
156- console .info (f"Creating app for testing: { demo_app_dir } " )
156+ console .info (f"Creating app for testing: { demo_app_dir !s } " )
157157
158- os . makedirs ( demo_app_dir )
158+ demo_app_dir . mkdir ( exist_ok = True )
159159
160160 with set_directory (demo_app_dir ):
161161 # We start with the blank template as basis.
162162 _init (name = demo_app_name , template = constants .Templates .DEFAULT )
163163 # Then overwrite the app source file with the one we want for testing custom components.
164164 # This source file is rendered using jinja template file.
165- with open (f"{ demo_app_name } /{ demo_app_name } .py" , "w" ) as f :
166- f .write (
167- templates .CUSTOM_COMPONENTS_DEMO_APP .render (
168- custom_component_module_dir = name_variants .custom_component_module_dir ,
169- module_name = name_variants .module_name ,
170- )
165+ demo_file = Path (f"{ demo_app_name } /{ demo_app_name } .py" )
166+ demo_file .write_text (
167+ templates .CUSTOM_COMPONENTS_DEMO_APP .render (
168+ custom_component_module_dir = name_variants .custom_component_module_dir ,
169+ module_name = name_variants .module_name ,
171170 )
171+ )
172172 # Append the custom component package to the requirements.txt file.
173- with open (f"{ constants .RequirementsTxt .FILE } " , "a" ) as f :
173+ with Path (f"{ constants .RequirementsTxt .FILE } " ). open ( mode = "a" ) as f :
174174 f .write (f"{ name_variants .package_name } \n " )
175175
176176
@@ -296,13 +296,14 @@ def _populate_custom_component_project(name_variants: NameVariants):
296296 )
297297
298298 console .info (
299- f"Initializing the component directory: { CustomComponents .SRC_DIR } / { name_variants .custom_component_module_dir } "
299+ f"Initializing the component directory: { CustomComponents .SRC_DIR / name_variants .custom_component_module_dir } "
300300 )
301- os . makedirs ( CustomComponents .SRC_DIR )
301+ CustomComponents .SRC_DIR . mkdir ( exist_ok = True )
302302 with set_directory (CustomComponents .SRC_DIR ):
303- os .makedirs (name_variants .custom_component_module_dir )
303+ module_dir = Path (name_variants .custom_component_module_dir )
304+ module_dir .mkdir (exist_ok = True , parents = True )
304305 _write_source_and_init_py (
305- custom_component_src_dir = name_variants . custom_component_module_dir ,
306+ custom_component_src_dir = module_dir ,
306307 component_class_name = name_variants .component_class_name ,
307308 module_name = name_variants .module_name ,
308309 )
@@ -814,7 +815,7 @@ def _validate_project_info():
814815 )
815816 pyproject_toml ["project" ] = project
816817 try :
817- with open ( CustomComponents .PYPROJECT_TOML , "w" ) as f :
818+ with CustomComponents .PYPROJECT_TOML . open ( "w" ) as f :
818819 tomlkit .dump (pyproject_toml , f )
819820 except (OSError , TOMLKitError ) as ex :
820821 console .error (f"Unable to write to pyproject.toml due to { ex } " )
@@ -922,16 +923,15 @@ def _validate_url_with_protocol_prefix(url: str | None) -> bool:
922923def _get_file_from_prompt_in_loop () -> Tuple [bytes , str ] | None :
923924 image_file = file_extension = None
924925 while image_file is None :
925- image_filepath = console . ask (
926- "Upload a preview image of your demo app (enter to skip)"
926+ image_filepath = Path (
927+ console . ask ( "Upload a preview image of your demo app (enter to skip)" )
927928 )
928929 if not image_filepath :
929930 break
930- file_extension = image_filepath .split ( "." )[ - 1 ]
931+ file_extension = image_filepath .suffix
931932 try :
932- with open (image_filepath , "rb" ) as f :
933- image_file = f .read ()
934- return image_file , file_extension
933+ image_file = image_filepath .read_bytes ()
934+ return image_file , file_extension
935935 except OSError as ose :
936936 console .error (f"Unable to read the { file_extension } file due to { ose } " )
937937 raise typer .Exit (code = 1 ) from ose
0 commit comments