Return Types
In addition to arbitrary Python values, Hyperscript can also return files
from hyperscript import Hyperscriptfrom hyperscript.convergence import Convergenceimport os
def hello_world_file(name): from hyperscript.File import File with open("greeting.txt", "w") as f: f.write(f"Hello, {name}!") return File("greeting.txt") # Return a file object to indicate we want to return a file
async def main(): # Set up Convergence as our backend convergence = Convergence(api_key=os.getenv("CONVERGENCE_API_KEY"))
# Initialize Hyperscript with Convergence's storage and event systems async with Hyperscript( storage=convergence.storage, events=convergence.events ) as hs: # Run the hello_world function in the cloud task = await hs.run(fn=hello_world_file, args=["Cammy"], executor=convergence.executor)
# Instead of calling task.results, we can use task.files() to get the returned files async for file in task.files(): print(f"Got file: {file.name}") # We can also download the file convergence.storage.download(src=file, path=f"./downloaded_{file.name}")
if __name__ == "__main__": import asyncio
asyncio.run(main())