Me dicen josu
Me gusta programar
Y les vengo a contar cosas que aprendi metiendole tailwindcss y live reloading para una aplicacion en flask
Sabias que colorear la terminal es tan sencillo como agregar unos caracteres antes del texto?
print("\033[1;31mHola Mundo")
print("\033[1;31mHola Mundo")

se puede hacer mejor?
SI
class Term:
if platform.system() == 'Windows':
os.system('color')
BLACK = "\033[30m"
R = "\033[31m"
G = "\033[32m"
BG = "\033[1;32m"
Y = "\033[33m"
B = "\033[34m"
M = "\033[35m"
C = "\033[36m"
W = "\033[37m"
ENDC = "\033[0;0m"
NORMAL = "\033[1m"
BOLD = "\033[1m"
def dev_print(*values: object):
print(f'{Term.M}[dev]{Term.ENDC}', *values)
Usandolo:
def minify_tailwindcss(cli: argparse.Namespace):
...
dev_print(f'Minifying tailwindcss for production...')
build_result = subprocess.run(shlex.split(command))
if build_result.returncode != 0:
dev_print(
f'Tailwind build for production {Term.R}fail{Term.ENDC}')
return build_result.returncode
dev_print(
f'Tailwind build for production {Term.G}ready{Term.ENDC}')
return build_result.returncode
Salio bien
Salio mal
Chat gpt, copilot y toda sus amigos ia estan buenos
trigger_full_reload = False
async def send_full_reload(websocket):
global trigger_full_reload
while True:
if trigger_full_reload:
trigger_full_reload = False
await websocket.send(json.dumps({
"type": "TRIGGER_FULL_RELOAD",
"data": datetime.datetime.now().isoformat()
}))
await asyncio.sleep(REFRESH_RATE_SEC)
# en otro lugar...
trigger_full_reload = True
el problema es que esa boleana solo funcionaba una vez
chatgpt: hmmm nose
copilot: me mataste
DOCUMENTACION
LR_CONNECTIONS: 'set[ws_server.WebSocketServerProtocol]' = set()
async def handle_connection(websocket):
LR_CONNECTIONS.add(websocket)
try:
await websocket.wait_closed()
finally:
LR_CONNECTIONS.remove(websocket)
# en otro lugar...
ws_protocol.broadcast(LR_CONNECTIONS, json.dumps({
"type": "TRIGGER_FULL_RELOAD",
"data": datetime.datetime.now().isoformat()
}))
o bueno hasta que un bug me mande a leerla denuevo...
ya paso.Lo facil esta bueno no?, no???
tenemos la lib argparse
en la std de python
import argparse
print("\033[1;31mHola Mundo")
parser = argparse.ArgumentParser(
description='Process some integers.')
parser.add_argument(
'integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument(
'--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
args = parser.parse_args()
print(args.accumulate(args.integers))

import argparse
print("\033[1;31mHola Mundo")
...
import argparse
print("\033[1;31mHola Mundo\033[0;0m")
...

yo: uh para, entonces vamos a agregar alguna que otra opcion
una que otra?
def cli() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description='Enhanced dev environment for flask apps.',
allow_abbrev=True
)
# +75 lineas despues
parser_dev.add_argument(
'-tm', '--tailwind-minify', dest='tailwind_minify', action='store_true', default=False,
help='Enables minification of the generated css file.'
)
return parser
recomendacion, no se emocionen
Un paron aveces ayuda
...
while True:
if trigger_full_reload:
trigger_full_reload = False
await websocket.send(json.dumps({
"type": "TRIGGER_FULL_RELOAD",
"data": datetime.datetime.now().isoformat()
}))
...
websockets.broadcast(websockets, message, raise_exceptions=False)
Broadcast a message to several WebSocket connections.
...
while True:
if trigger_full_reload:
trigger_full_reload = False
await websockets.send(json.dumps({
"type": "TRIGGER_FULL_RELOAD",
"data": datetime.datetime.now().isoformat()
}))
...

los docs dicen websockets
no websocket
y los confundi
Dormir esta bien
pero se me hizo tarde
mejor si me voy a dormir
y recuerden, si una idea se les ocurre, por mas rara, simple o dificil sea
si piensan que minimamente es factible
intentalo
aunque no lo consigas, seguro algo aprendes
yo ahora se colorear la terminal 😎
Link al repositorio