2323from .utils .cli import (
2424 get_rich_toolkit ,
2525 get_uvicorn_log_config ,
26+ should_use_rich_toolkit ,
2627 should_use_rich_uvicorn_logs ,
2728)
2829
@@ -143,15 +144,20 @@ def _run(
143144 forwarded_allow_ips : str | None = None ,
144145 public_url : str | None = None ,
145146) -> None :
146- with get_rich_toolkit () as toolkit :
147+ use_rich = should_use_rich_toolkit ()
148+ with get_rich_toolkit (use_rich = use_rich ) as toolkit :
147149 server_type = "development" if command == "dev" else "production"
148150
149- toolkit .print_title (f"Starting { server_type } server 🚀" , tag = "FastAPI" )
150- 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" )
151155
152- toolkit .print (
153- "Searching for package file structure from directories with [blue]__init__.py[/blue] files"
154- )
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+ )
155161
156162 if entrypoint and (path or app ):
157163 toolkit .print_line ()
@@ -197,69 +203,77 @@ def _run(
197203 module_data = import_data .module_data
198204 import_string = import_data .import_string
199205
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" )
206+ if use_rich :
207+ toolkit .print (f"Importing from { module_data .extra_sys_path } " )
207208 toolkit .print_line ()
208209
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- )
210+ if module_data .module_paths :
211+ root_tree = _get_module_tree (module_data .module_paths )
223212
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 } " )
213+ toolkit .print (root_tree , tag = "module" )
214+ toolkit .print_line ()
233215
234- 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+ )
235220 toolkit .print_line ()
236221 toolkit .print (
237- "You can configure an entrypoint in [blue]pyproject.toml[/] for this app with:" ,
238- tag = "tip" ,
222+ f"[underline]from [bold]{ module_data .module_import_str } [/bold] import [bold]{ import_data .app_name } [/bold]"
239223 )
240224 toolkit .print_line ()
225+
241226 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- )
227+ f"Using import string: [blue]{ import_string } [/]" ,
228+ tag = "app" ,
250229 )
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+ )
251261
252262 url = public_url .rstrip ("/" ) if public_url else f"http://{ host } :{ port } "
253263 url_docs = f"{ url } /docs"
254264
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- )
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 ("" )
261275
262- if command == "dev" :
276+ if command == "dev" and use_rich :
263277 toolkit .print_line ()
264278 toolkit .print (
265279 "Running in development mode, for production use: [bold]fastapi run[/]" ,
@@ -271,9 +285,10 @@ def _run(
271285 "Could not import Uvicorn, try running 'pip install uvicorn'"
272286 ) from None
273287
274- toolkit .print_line ()
275- toolkit .print ("Logs:" )
276- toolkit .print_line ()
288+ if use_rich :
289+ toolkit .print_line ()
290+ toolkit .print ("Logs:" )
291+ toolkit .print_line ()
277292
278293 uvicorn_kwargs : dict [str , Any ] = {
279294 "app" : import_string ,
0 commit comments