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