embedding jupyter notebook/ google colab in Django app - python

I wanted to build a website and embed the jupyter notebook functionality of being able to create cells and run python code within it into my website
For creating a website I m using Django
and I would like to embed either the google collab or jupyter notebook
By the way I have researched enough and have been stuck with the StackOverflow links where there no answer about this or the one where they want to use django in jupyter notebook
Thanks in advance for any guidance or any reference that you guys can provide.

You have many options to do that:
Note:: I used "jupyter-lab" you can use "jupyter notebook"
1- The first option to redirect to "jupyter notebook"
django view.py
from django.shortcuts import redirect,HttpResponse
import subprocess
import time
def open_jupiter_notbook(request):
b= subprocess.check_output("jupyter-lab list".split()).decode('utf-8')
if "9999" not in b:
a=subprocess.Popen("jupyter-lab --no-browser --port 9999".split())
start_time = time.time()
unreachable_time = 10
while "9999" not in b:
timer = time.time()
elapsed_time = timer-start_time
b= subprocess.check_output("jupyter-lab list".split()).decode('utf-8')
if "9999" in b:
break
if elapsed_time > unreachable_time:
return HttpResponse("Unreachable")
path = b.split('\n')[1].split('::',1)[0]
#You can here add data to your path if you want to open file or anything
return redirect(path)
if you want to implement it in template instead of redirect, you can use the following code in Django template:
<iframe src="{% url 'open_jupiter_notbook' %}" width= 600px height=200px></iframe>
2- The second option:
just use jupyter notebook commands
by using this subprocess.check_output("your command".split())

Export it first and then import it, look here for a good explanation
from google.colab import files
src = list(files.upload().values())[0]
open('mylib.py','wb').write(src)
import mylib

Related

Autoplotter Python Library Integraton with Django Python Web framework

I have seen a tutorial here Which is demonstrating the data analysis in the jupyter notebook cell, I am looking for the solution that how can i show the output of autoplotter which is python library in the django templates. Below is the code snippet of autoplotter which i have taken from its official website:
from autoplotter import run_app # Importing the autoplotter for GUI Based EDA
import pandas as pd # Importing Pandas to read csv
df = pd.read_csv("https://raw.githubusercontent.com/ersaurabhverma/autoplotter/master/Data/data.csv") # Reading data
run_app(df,mode = "inline", host="127.0.0.1", port=5000) # Calling the autoplotter.run_app in inline mode
run_app(df,mode = "external", host="127.0.0.1", port=5000) # Calling the autoplotter.run_app in external mode
I am looking for that what is the output format of this command
run_app (dataframe, host, port....)
how can I display its output in my django templates? so that a person could interact with his data through my launched website? Looking for any smart solution. Thank you
You can't run it under Django project because autoplotter uses Flask to serve the data and Flask can work only in the main thread of the main interpreter.
However, you can solve your problem using Docker. You will have a separate service that serves the autoplotter app and Django can have an iframe in HTML template that shows the content of the service.
UPD:
For the Docker - you can start with this guide. The only difference will be in your case is that app.py will contain only the call for a plotter:
if __name == '__main__':
run_app(df, mode="external", host="127.0.0.1", port=5000)
And requirements.txt with autoplotter

How to tell flask to random picture from directory everytime user refresh web page?

Currently I am using flask and heroku to deploy my website but i need my webpage show random photo from img directory currently my code look like this
import os
import random
imgs = os.listdir('static/img')
imgs = ['img/' + file for file in imgs]
imgrand = random.sample(imgs,k=5)
#app.route('/')
def index():
return render_template('index.html', imgrand=imgrand)
And my HTML code look like this
{% for img in imgrand %}
<img src="static/{{img}}" alt="{{imgL}}" style="width:100%">
{% endfor %}
So long it work fine in my local machine ,but it only random picture only once when start flask run command in terminal. My goal is to make my web page random picture everytime when refresh webpage without going to end terminal session and start flask run command all over again.
In this case imggrand variable only takes one sample at the beggining of the program, for repeating the process everytime you refresh the webpage you need to put imggrand inside the endpoint.
I recommend you to learn about endpoints and designing the workflow starting with this: https://flask.palletsprojects.com/en/1.1.x/quickstart/#rendering-templates

How to create a table with clickable hyperlink to a local file in pandas & Jupyter Notebook

I learned from this post that I can link to a website in a Jupyter Notebook:
How to create a table with clickable hyperlink in pandas & Jupyter Notebook
So, I tried to adapt the code to create a dataframe with links to local files. However, when I click on the hyperlinks from the code below, nothing happens.
How do I fix the code below for the hyperlinks to work?
import os
import pandas as pd
data = [dict(name='file1',
filepath='C:/Users/username/Documents/file1.docx'),
dict(name='file2',
filepath='C:/Users/username/Documents/file2.docx')]
df = pd.DataFrame(data)
def make_clickable(url):
name= os.path.basename(url)
return '{}'.format(url,name)
df.style.format({'filepath': make_clickable})
Your browser is actually blocking this. You probably see an error message like "Not allowed to load local resource" in your browser's developer tools (Chrome, Firefox, Safari). Changing this would expose you to serious security risks.
An alternative could be to put the files you want to access in the same working directory as your Jupyter Notebook. For instance, if you add a folder named "Documents" in your working directory, you can then link to the files like this:
http://localhost:8888/notebooks/Documents/file1.docx
Your code would be:
import os
import pandas as pd
data = [dict(name='file1',
filepath='Documents/file1.docx'),
dict(name='file2',
filepath='Documents/file2.docx')]
df = pd.DataFrame(data)
def make_clickable(url):
name= os.path.basename(url)
return '{}'.format(url,name)
df.style.format({'filepath': make_clickable})

Django Export data from postGre to shapeFile

I'm new in django and python.
I've tried to export database data to shapefile using https://bitbucket.org/springmeyer/django-shapes/src
and i got a Segmentation fault. is there is anyway to make this work by only using django GeoDjango?
That's a non-updated project. You can check this a bit more up to date fork.
To check quickly if it works for you download the zip and copy the shape-engine folder inside your django project.
Install fiona (pip install fiona)
Add a new url to download the shapfile like
from .views import export
urlpatterns = urlpatterns + [ url(r'^worldshapes/', export_worldshapes), ]
And a new view:
from shape_engine.shape_responder import ShpResponder
def export(request):
from .models import WorldBorders
w = WorldBorders.objects.all()
shp_response = ShpResponder(w)
shp_response.file_name = 'World Borders'
return shp_response()
django-shape-engine only works in python 2.x. A couple of changes should be made to work with it in python 3. Basically, use BytesIO instead of StringIO

IPython: Configure Base Url Path for All Request

I am trying to figure out how to configure the base url of and IPython notebook server running. So instead of the default:
#request# GET http://localhost:8888/static/tree/js/main.min.js?v=04a28c5e21950738efb217191f08ac33
#request# GET http://localhost:8888/api/terminals?_=1441754529652
#request# GET http://localhost:8888/custom/custom.js?v=20150908160654
#request# GET http://localhost:8888/notebooks/Untitled1.ipynb?kernel_name=python3#
I want to configure all requests so that the go through ipython, as in:
#request# GET http://localhost:8888/ipython/static/tree/js/main.min.js?v=04a28c5e21950738efb217191f08ac33
#request# GET http://localhost:8888/ipython/api/terminals?_=1441754529652
#request# GET http://localhost:8888/ipython/custom/custom.js?v=20150908160654
#request# GET http://localhost:8888/ipython/notebooks/Untitled1.ipynb?kernel_name=python3#
Is this possible?
To change the base url for the files that are being served up from iPython, edit the ipython_notebook_config.py file in your ~/.ipython/[profile-name]/ directory.
In particular, assuming that your config file starts with the line c = get_config(), you will want to add the following lines to your configuration:
c.NotebookApp.base_project_url = '/ipython/'
c.NotebookApp.base_kernel_url = '/ipython/'
c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'}
This will make it so that your project is served up from http://localhost:8888/ipython/ instead of http://localhost:8888/.
For more information please see this page of the ipython docs.

Categories

Resources