Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save fomightez/53a8e6153095402fe2f168789224099c to your computer and use it in GitHub Desktop.

Select an option

Save fomightez/53a8e6153095402fe2f168789224099c to your computer and use it in GitHub Desktop.
Useful snippets and examples for use of JupyterLite with commands typically related to the shell available in standard Jupyter
#Useful snippets and examples for use of JupyterLite with commands typically related to the shell available in standard Jupyter
# (Also see 'Useful snippets and examples for when converting command line commands from Jupyter/IPython back to Pure Python' at
# https://gist.github.com/fomightez/ed79e33e97601d839dd550fd224d583c because a lot of these overlap, yet JupyterLite has some magics that obviously don't work with Pure Python
# MAGIC COMMANDS THAT SHOULD WORK IN JUPYTERLITE
%pwd
%store s >test_store.txt # if `s` previously define.
#`%ls` fails at present, but you can do
import os
os.listdir()
# Pure Python of the above magic commands that work in JupyterLite so don't need to look elsewhere
# pwd
from pathlib import Path
Path.cwd()
#-or-
import os
os.getcwd()
# ls
import os
os.listdir()
# List specific directory
os.listdir("/home/pyodide")
# List current directory
from pathlib import Path
list(Path.cwd().iterdir())
# Or more readable
from pathlib import Path
for item in Path.cwd().iterdir():
print(item.name)
# %store
# This one doesn't have any direct equivalent because depends on what you want to do and how you want to store. For
# example, Pandas and others can be pickled but you may simply want to save something as text or a csv or tsv, and
# so in that case see Useful snippets for Jupyter notebooks https://gist.github.com/fomightez/971d0958510c4c1a4734eafe4afa0cac
# that covers many of these.
# Pyodide-way to use curl or fetch that works in JupyterLite
import pyodide
url="https://raw.githubusercontent.com/jwbernin/PyRDA/main/sample-data/aim-datafile-1.csv"
file_content_string = pyodide.http.open_url(url).read()
with open(url.rsplit("/", 1)[-1], 'w') as output_file:
output_file.write(file_content_string)
#-or- with `requests`
import requests
def fetch_github_file_using_requests(the_url):
"""
Take a url for a raw GitHub file and return the file content using GitHub's CORS-enabled REST API endpoint
"""
response = requests.get(the_url)
response.raise_for_status() # Raise an exception for non-200 status codes
return response.text
url="https://raw.githubusercontent.com/jwbernin/PyRDA/main/sample-data/aim-datafile-1.csv"
file_content_string = fetch_github_file_using_requests(url)
with open(url.rsplit("/", 1)[-1], 'w', encoding='utf-8') as output_file:
output_file.write(file_content_string)
# Equivalent Pure Python of common shell commands that you'd use in Jupyter in a way that they'd in JupyterLite so don't need to look elsewhere
# cat
#When on last line of Jupyter Cell
from pathlib import Path
Path("file.txt").read_text()
#-or, when it won't be on last line of Jupyter Cell-
from pathlib import Path
print(Path("output.txt").read_text())
#-or-
with open("output.txt") as f:
print(f.read())
from IPython.display import Code
Code(filename="output.txt")
#-or-
from IPython.display import Code
Code(json.dumps(parsed, indent=4), language='json')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment