β οΈ THIS ARTICLE IS DEPRICATED AND IS BEING KEPT FOR ARCHIVAL PURPOSES.β οΈ
Please read everything found on the mainpage before continuing; disclaimer and all.
About this Tutorial:
note: There is a lot of quoting and paraphrasing on this page in particular. Be sure to check the links you want to learn more.
We can update it in-drive and then rerun the results in near-real-time.
Several post-render functions will be used to reduce network requests.
I intend to hook this up to our webpack scripts for all the benefits.
FNS
These scripts will convert an nb to an html doc. NBdev and Marko among other libraries are used.
Misc Notes
Unsorted notes from previous nb
# cell_type: 'code', # metadata<{colab{base_uri, height}, outputid}>, # outputs[ # { name"stdout", output_type"stream", text"FutureWarning: Method `add_children` is deprecated"}, # {, output_type"execute_result", 'data': {'text/html': 'thisistheactualmap!','text/plain': ''}, execution_count, metadata{tags} # ] #], source"#hide_input\n#collapse_output\n makefoliummap(data)" }
- display_data = ['text/html', 'text/plain']
- stream
- execute_result = ['image/jpeg', 'text/plain']
- error
%html
- {'output_type': 'display_data', 'data': {'text/html': <script console.log('hi')\n script>", 'text/plain': '<IPython.core.display.HTML object>'} }
raw markdown
- {'output_type': 'stream', 'text': 'Thisis text \n'}
display( airbnb.head() ); Image(filename='test.jpg')
- {'output_type': 'display_data', 'data': {'text/html': div>oooAKKKKAP/Z</div', 'text/plain': '<IPython.core.display.Image object>'}
Convert FN's
This next bit will run the conversion on a single file.
import requests from google.colab import output t= """ outp = runit() v = 0 if not 'v' in globals() else v+1 # page = f"index{v}.html" page = f"index.html" print(page) f = open(page, "a") f.write( ''.join( [''.join(c) for c in outp] ) ) f.close() # posted = requests.get( 'https://charleskarpati.com/version.php', params=[('v', v)], ).json() # IPython.display.HTML( outp ) """
Serve Files with Ngrok
You can test on the Colab by using a flask server which can be a bit tricky.
from flask import Flask from numpy import random import requests app = Flask(__name__) run_with_ngrok(app) #starts ngrok when the app is run def getHtml(filepath): with open(filepath, 'r') as file: data = file.read().replace('\n', '') return data @app.route("/") def index(): # Get version number using the localstorage. # v = requests.get( 'https://charleskarpati.com/version.php', ).json() # location = f'index{v}.html' # print('\n \n LOCATION: ', location) # return getHtml(location) outp = runit('BniaScripts.ipynb') resp = ''.join( [''.join(c) for c in outp] ) resp = resp.replace('', '') return resp @app.route("/about") def about(): return getHtml('lines.html') app.run()
Working with GDrive Directories, Github, NBDev
Connect to Drive
If you have content on your Google Drive, you can publish entire directorys.
!pip install nbdev !pip install flask_ngrok from google.colab import drive drive.mount('/content/drive') %cd /content/drive/My Drive/'Software Development Documents'/dataplay
%cd ../{project}/
from IPython.core.display import display, HTML
SETUP
def addAfterNav(title): return f""""""""" addToFooter = """{title}
if (project == 'projectDocumentation'): navMid = """
"""
Run one
from flask import Flask from numpy import random import requests header = requests.get('https://bniajfi.org/wp-json/wp/v2/header') header = header.content.decode('utf-8')[:-4] header= header.replace('/wp-content/uploads/2021/02/bnia_text.png', 'https://bniajfi.org/wp-content/uploads/2021/02/bnia_text.png') header = header + addToHeader + nav footer = requests.get('https://bniajfi.org/wp-json/wp/v2/footer') footer = addToFooter+footer.content.decode('utf-8')[:-4] app = Flask(__name__) run_with_ngrok(app) #starts ngrok when the app is run o = """ def getHtml(filepath): with open(filepath, 'r') as file: data = file.read().replace('\n', '') return data """ @app.route("/") def index(): def template(title, x): return f'{header}{title}{x}{footer}' def replace(txt): txt = txt.replace('', '') return txt # Get version number using the localstorage. # v = requests.get( 'https://charleskarpati.com/version.php', ).json() # location = f'index{v}.html' # print('\n \n LOCATION: ', location) # return getHtml(location) infilename = 'index.ipynb' outp = runit(infilename) title = "".join(re.findall('[a-zA-Z][^a-zA-Z]*', 'index.ipynb'[:-6])) resp = ''.join( [''.join(c) for c in outp] ) resp = replace(resp) resp = template( addAfterNav(title) , resp) return resp @app.route("/about") def about(): return getHtml('lines.html') app.run()
Run all
%cd ../{project}/
import os def template(title, x): return f'{header}{title}{x}{footer}' def replace(txt): txt = txt.replace('', '') return txt # traverse whole directory for file in os.listdir(r'notebooks'): # check the extension of files if (file.endswith('.ipynb') ): # print whole path of files print(os.path.join('notebooks', file)) title = "".join(re.findall('[a-zA-Z][^a-zA-Z]*', file[:-6])) page = f"./docs/{title}.html" outp = runit(os.path.join('notebooks', file)) resp = ''.join( [''.join(c) for c in outp] ) resp = replace(resp) resp = template( addAfterNav(title) , resp) try: f = open(page, "a") except: f = open(page[1:], "a") f.write( resp ) f.close() # send it to my websesrver for saving.
Publishing a new Repo
%cd ../{project}/
! git init ! git config --global user.email "bniajfi@gmail.com" ! git config --global user.name "bniajfi"
! git add * ! git commit -m "Second commit for styling" ! git branch -M main
tokenVS = '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' token = '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' username = 'BNIA' print(f'https://{token}@github.com/{username}/{project}.git') ! git remote remove origin ! git remote add origin 'https://{token}@github.com/{username}/{project}.git' #! git push -u origin main !git push 'https://{token}@github.com/{username}/{project}.git'
Pypi
! pip install twine ! nbdev_bump_version ! make pypi
The βsrc refspec master does not match anyβ error occurs if you have forgotten to add the files you have changed to a commit and try to push those changes to a remote repository before you make the first commit in your repository.
'https://stackoverflow.com/questions/61424599/error-when-pushing-files-to-git-in-colab-fatal-could-not-read-username-for-ht' 'https://stackoverflow.com/questions/492558/removing-multiple-files-from-a-git-repo-that-have-already-been-deleted-from-disk' 'https://github.community/t/error-refname-refs-heads-master-not-found/144737/13' 'https://github.com/settings/tokens' 'https://github.com/settings/tokens' 'https://stackoverflow.com/questions/61424599/error-when-pushing-files-to-git-in-colab-fatal-could-not-read-username-for-ht'
null """
class="span8 content" """
<- end content div
<- end section
<- end container
"""