this is a circuitpython example of updating a text file using the neocities api - as used on /micromicroblog

based on uart_demo.py by arturo182

   
from bbq10keyboard import BBQ10Keyboard, STATE_PRESS, STATE_RELEASE, STATE_LONG_PRESS
from adafruit_display_text.label import Label
from adafruit_display_shapes.rect import Rect
import adafruit_ili9341
import terminalio
import displayio
import board
import busio
import time
import neopixel
import adafruit_requests as requests
import wifi
import ssl
import socketpool
import pwmio

from secrets import secrets
wifi.radio.connect(secrets["ssid"], secrets["password1"])
pool = socketpool.SocketPool(wifi.radio)
requests = requests.Session(pool, ssl.create_default_context())
print('internet connect')

pwm = pwmio.PWMOut(board.A0, frequency=50)

# BUZZ
pwm.duty_cycle = 2 ** 15  # Cycles the pin with 50% duty cycle (half of 2 ** 16) at 50hz
time.sleep(.2)
pwm.duty_cycle= 43253
time.sleep(.2)
pwm.duty_cycle= 0

#  ui dimensions
header = 32
margin = 8
border = 1

# Release any resources currently in use for the displays
displayio.release_displays()

spi = board.SPI()
tft_cs = board.D21
tft_dc = board.D20

spi = board.SPI()
tft_cs = board.D5
tft_dc = board.D6
touch_cs = board.D21
sd_cs = board.D20
neopix_pin = board.D9

display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs)
display = adafruit_ili9341.ILI9341(display_bus, width=320, height=240)

i2c = board.I2C()
kbd = BBQ10Keyboard(i2c)

splash = displayio.Group()
display.show(splash)

# background
def bg_stripe(x, color):
    width = display.width // 8
    color_bitmap = displayio.Bitmap(width, 240, 1)
    color_palette = displayio.Palette(1)
    color_palette[0] = color
    bg_sprite = displayio.TileGrid(color_bitmap, x=x*width, y=0, pixel_shader=color_palette)
    splash.append(bg_sprite)

bg_stripe(0, 0x000000)
bg_stripe(1, 0x784F17)
bg_stripe(2, 0x86007E)
bg_stripe(3, 0x86007D)
bg_stripe(4, 0xFFFF61)
bg_stripe(5, 0x86007D)
bg_stripe(6, 0x0060F9)
bg_stripe(7, 0x86007D)

# output rect
output_rect = Rect(margin, margin, display.width-margin*2, display.height-margin*2-header-margin, fill=0xFFFFFF, outline=0x666666)
splash.append(output_rect)

output_text = Label(terminalio.FONT, text="...", x=margin*2+border, y=int(margin+border+header+5), color=0x000000)
splash.append(output_text)

# output header
header_rect = Rect(margin + border, margin+border, display.width-(margin+border)*2, header, fill=0xCCCCCC)
splash.append(header_rect)

header_text = Label(terminalio.FONT, text="micromicroblogging", x=margin*2+border, y=int(margin+border+header/2), color=0x000000)
splash.append(header_text)

# output text
p = displayio.Palette(2)
p.make_transparent(0)
p[1] = 0x000000

w, h = terminalio.FONT.get_bounding_box()
tilegrid = displayio.TileGrid(terminalio.FONT.bitmap, pixel_shader=p, x=margin*2+border, y=int(margin+border+header+margin/2), width=48, height=10, tile_width=w, tile_height=h)
term = terminalio.Terminal(tilegrid, terminalio.FONT)
splash.append(tilegrid)

# input textarea
input_rect = Rect(margin, display.height-margin-header, display.width-margin*2, header, fill=0xFFFFFF, outline=0x666666)
splash.append(input_rect)

# input text
input_text = Label(terminalio.FONT, text='', x=margin*2+border, y=int(display.height-margin-border-header*0.7), color=0x000000)
splash.append(input_text)

# carret
carret = Rect(input_text.x + input_text.bounding_box[2] + 1, int(display.height-margin-header/2-header/4), 1, header//2, fill=0x000000)
splash.append(carret)

carret_blink_time = time.monotonic()
carret_blink_state = True

while True:
    # Carret blink animation
    if time.monotonic() - carret_blink_time >= 0.5:
        if carret_blink_state:
            splash.remove(carret)
        else:
            splash.append(carret)

        carret_blink_state = not carret_blink_state
        carret_blink_time = time.monotonic()

    # Process keyboard
    if kbd.key_count > 0:
        k = kbd.key
        if k[0] == STATE_RELEASE:
            if k[1] == '\x08': # Backspace
                if len(input_text.text) > 0:
                    input_text.text = input_text.text[:-1]
            elif k[1] == '\n': # Enter, send micromicroblog
                text =  input_text.text + '\n'
                print(input_text.text.encode('utf-8'))
                filenumber="1"
                print("Fetching text from", 'https://sowe.li/micromicroblog/1.txt')
                response = requests.get('https://sowe.li/micromicroblog/1.txt')
                print("-" * 40)
                print(response.text)
                print("-" * 40)
                url1 = 'https://neocities.org/api/upload'
                payload= '--CaseyCaseyCaseyCasey\r\nContent-Disposition: form-data; name="micromicroblog/'+filenumber+'.txt"; filename="micromicroblog/'+filenumber+'.txt"\r\n\r\n'" "+str(input_text.text.encode('utf-8').decode("utf-8"))+"\n\n"+str(response.text)+""'\r\n--CaseyCaseyCaseyCasey--\r\n'
                headers={"Authorization": "Basic " + secrets["apikey"], "Content-Type": 'multipart/form-data; boundary=CaseyCaseyCaseyCasey'}
                
                response = requests.post(url1, data=payload, headers=headers)
                print('post success')
                print("sowe.li/thought/"+filenumber+".txt")
                output_text.text = input_text.text + "\n" + output_text.text 
                input_text.text = ''
                pwm.duty_cycle = 2 ** 15  # Cycles the pin with 50% duty cycle (half of 2 ** 16) at 50hz
                time.sleep(.2)
                pwm.duty_cycle= 43253
                time.sleep(.2)
                pwm.duty_cycle= 0
            else: # Anything else, we add to the text field
                input_text.text += k[1]
                
            carret.x = input_text.x + input_text.bounding_box[2] + 1