@@ -143,15 +143,20 @@ def _run(
143143 forwarded_allow_ips : str | None = None ,
144144 public_url : str | None = None ,
145145) -> None :
146- with get_rich_toolkit () as toolkit :
146+ use_rich = should_use_rich_logs ()
147+ with get_rich_toolkit (use_rich = use_rich ) as toolkit :
147148 server_type = "development" if command == "dev" else "production"
148149
149- toolkit .print_title (f"Starting { server_type } server 🚀" , tag = "FastAPI" )
150- toolkit .print_line ()
150+ if use_rich :
151+ toolkit .print_title (f"Starting { server_type } server 🚀" , tag = "FastAPI" )
152+ else :
153+ toolkit .print_title ("⚡️ Starting FastAPI" )
151154
152- toolkit .print (
153- "Searching for package file structure from directories with [blue]__init__.py[/blue] files"
154- )
155+ if use_rich :
156+ toolkit .print_line ()
157+ toolkit .print (
158+ "Searching for package file structure from directories with [blue]__init__.py[/blue] files"
159+ )
155160
156161 if entrypoint and (path or app ):
157162 toolkit .print_line ()
@@ -197,69 +202,77 @@ def _run(
197202 module_data = import_data .module_data
198203 import_string = import_data .import_string
199204
200- toolkit .print (f"Importing from { module_data .extra_sys_path } " )
201- toolkit .print_line ()
202-
203- if module_data .module_paths :
204- root_tree = _get_module_tree (module_data .module_paths )
205-
206- toolkit .print (root_tree , tag = "module" )
205+ if use_rich :
206+ toolkit .print (f"Importing from { module_data .extra_sys_path } " )
207207 toolkit .print_line ()
208208
209- toolkit .print (
210- "Importing the FastAPI app object from the module with the following code:" ,
211- tag = "code" ,
212- )
213- toolkit .print_line ()
214- toolkit .print (
215- f"[underline]from [bold]{ module_data .module_import_str } [/bold] import [bold]{ import_data .app_name } [/bold]"
216- )
217- toolkit .print_line ()
218-
219- toolkit .print (
220- f"Using import string: [blue]{ import_string } [/]" ,
221- tag = "app" ,
222- )
209+ if module_data .module_paths :
210+ root_tree = _get_module_tree (module_data .module_paths )
223211
224- mod_source_desc = SOURCE_DESCRIPTIONS [import_data .module_config_source ]
225- app_source_desc = SOURCE_DESCRIPTIONS [import_data .app_name_config_source ]
226- toolkit .print_line ()
227- toolkit .print ("Configuration sources:" , tag = "info" )
228- if mod_source_desc == app_source_desc :
229- toolkit .print (f" • Import string: { mod_source_desc } " )
230- else :
231- toolkit .print (f" • Module: { mod_source_desc } " )
232- toolkit .print (f" • App name: { app_source_desc } " )
212+ toolkit .print (root_tree , tag = "module" )
213+ toolkit .print_line ()
233214
234- if import_data .module_config_source == "auto-discovery" :
215+ toolkit .print (
216+ "Importing the FastAPI app object from the module with the following code:" ,
217+ tag = "code" ,
218+ )
235219 toolkit .print_line ()
236220 toolkit .print (
237- "You can configure an entrypoint in [blue]pyproject.toml[/] for this app with:" ,
238- tag = "tip" ,
221+ f"[underline]from [bold]{ module_data .module_import_str } [/bold] import [bold]{ import_data .app_name } [/bold]"
239222 )
240223 toolkit .print_line ()
224+
241225 toolkit .print (
242- Syntax (
243- (
244- "[tool.fastapi]\n "
245- f'entrypoint = "{ import_data .module_data .module_import_str } :{ import_data .app_name } "'
246- ),
247- "toml" ,
248- theme = "ansi_light" ,
249- )
226+ f"Using import string: [blue]{ import_string } [/]" ,
227+ tag = "app" ,
250228 )
229+ else :
230+ toolkit .print (f"🐍 App: [blue]{ import_string } [/]" )
231+
232+ if use_rich :
233+ mod_source_desc = SOURCE_DESCRIPTIONS [import_data .module_config_source ]
234+ app_source_desc = SOURCE_DESCRIPTIONS [import_data .app_name_config_source ]
235+ toolkit .print_line ()
236+ toolkit .print ("Configuration sources:" , tag = "info" )
237+ if mod_source_desc == app_source_desc :
238+ toolkit .print (f" • Import string: { mod_source_desc } " )
239+ else :
240+ toolkit .print (f" • Module: { mod_source_desc } " )
241+ toolkit .print (f" • App name: { app_source_desc } " )
242+
243+ if import_data .module_config_source == "auto-discovery" :
244+ toolkit .print_line ()
245+ toolkit .print (
246+ "You can configure an entrypoint in [blue]pyproject.toml[/] for this app with:" ,
247+ tag = "tip" ,
248+ )
249+ toolkit .print_line ()
250+ toolkit .print (
251+ Syntax (
252+ (
253+ "[tool.fastapi]\n "
254+ f'entrypoint = "{ import_data .module_data .module_import_str } :{ import_data .app_name } "'
255+ ),
256+ "toml" ,
257+ theme = "ansi_light" ,
258+ )
259+ )
251260
252261 url = public_url .rstrip ("/" ) if public_url else f"http://{ host } :{ port } "
253262 url_docs = f"{ url } /docs"
254263
255- toolkit .print_line ()
256- toolkit .print (
257- f"Server started at [link={ url } ]{ url } [/]" ,
258- f"Documentation at [link={ url_docs } ]{ url_docs } [/]" ,
259- tag = "server" ,
260- )
264+ if use_rich :
265+ toolkit .print_line ()
266+ toolkit .print (f"Server started at [link={ url } ]{ url } [/]" , tag = "server" )
267+ toolkit .print (
268+ f"Documentation at [link={ url_docs } ]{ url_docs } [/]" , tag = "server"
269+ )
270+ else :
271+ toolkit .print (f"🌐 Server: [link={ url } ]{ url } [/]" )
272+ toolkit .print (f"📚 Docs: [link={ url_docs } ]{ url_docs } [/]" )
273+ toolkit .print ("" )
261274
262- if command == "dev" :
275+ if command == "dev" and use_rich :
263276 toolkit .print_line ()
264277 toolkit .print (
265278 "Running in development mode, for production use: [bold]fastapi run[/]" ,
@@ -271,9 +284,10 @@ def _run(
271284 "Could not import Uvicorn, try running 'pip install uvicorn'"
272285 ) from None
273286
274- toolkit .print_line ()
275- toolkit .print ("Logs:" )
276- toolkit .print_line ()
287+ if use_rich :
288+ toolkit .print_line ()
289+ toolkit .print ("Logs:" )
290+ toolkit .print_line ()
277291
278292 extra_uvicorn_kwargs = (
279293 get_uvicorn_log_config () if should_use_rich_logs () else {}
0 commit comments