How can I render HTML in a Jupyter Notebook? - python

When I browse my file system through a jupyter notebook server I can view and edit HTML and other text files in addition to ipynb files. However I want to view the files as rendered HTML instead of viewing them as a editable HTML.
In other words, how can I make a jupyter notebook server serve static content?

It already does, under /files. For example, when https://tmp39.tmpnb.org/user/IgoeEDdRLpRG/edit/featured/pandas-cookbook/README.md is the URL for editing a file, https://tmp39.tmpnb.org/user/IgoeEDdRLpRG/files/featured/pandas-cookbook/README.md is that file served up as-is (the first two segments of this example file path are specific to tmpnb servers).

Related

How can I run an html page from python using CGI

I am on a project where I have connected python and html using CGI. I am now able to receive inputs from html and save it to MySQL database through python. My project is a ticket booking website. I now have to show the user that their tickets have been booked, and also, I will have to show them their fare. Though I can simply type the code in printing statements to print the data in html, it doesn't look good from the user side, as I can't style it. Has anyone got any solutions? Can I directly start any html page like that?
I tried to print the fare, and I was able to do it. But the page looked very boring without any styling. I want to open an html page from my python file (because I can style it)-, but I am not using any web application like Flask or Django. How can I do it?
To run your Python script as a CGI script, you can either:
Name your script with a .cgi file extension (example.cgi).
Configure Apache to recognize and allow the .py file extension as a CGI script.
To run your python script as a CGI script,you can either:
Name your script with a .cgi file extension (example.cgi).
Configure Apache to recognize and allow the .py file extension as a CGI extension
"Run a HTML page" doen't make much sense. Sounds like your Python script is already running under CGI, and it's html output needs to include style information.
Print out the style information as well as the values into your html.
Or print out a link into the html header to include a seperate style sheet.
Or use python to load a html template from a file and make some string substitutions with your data before printing out the edited template.

How to open .ipynb pages as normal jupyter notebooks

I am learning python using a course. The course material can be found on the links like the following one:
http://faculty.washington.edu/sbrunton/me564/python/Python_Introduction.ipynb
I'd like to have the jupyter notebook when I go to the link but it shows the raw python file. How can I export the jupyter notebook from such links?
Thanks in advance for any help.
You can just open an already created jupyter notebook (the file with .ipynb) in a notepad and replace its text from the text in your link.
Steps
Create a totally new jupyter notebook project.
Go to the file location and open it with notepad
Remove all the content from the notepad
Replace it with the content in your link https://faculty.washington.edu/sbrunton/me564/python/Python_Introduction.ipynb
Save the notepad and close it.
Open the same file as a notebook using Jupyter notebook or Google Colab
You can copy the raw content and paste to local new file. File extension should
be .ipynb .Then you can open in jupyter lab or notebook.
Go to nbviewer.org, paste in the URL, and press 'Go!'. You'll then be redirected to here which is a page that has the following URL:
https://nbviewer.org/url/faculty.washington.edu/sbrunton/me564/python/Python_Introduction.ipynb
At that URL is the notebook rendering you seek. (nbviewer will even display some 'interactive' items such as Plotly plots and animated matplotlib plots backed by frames, examples here and here, respectively.)
Right-clicking the download icon in the upper right side of the notebook rendering there and selecting Save link As... will allow you to save the .ipynb file to your local machine. (You can do similar from the original page link, but there you have to edit the name. No editing of the name necessary this way for your link!)
If you examine the URL generated by the form, you'll note that there is a pattern based on what you provided. And so you could just change the original portion of the link you provided from http://... to https://nbviewer.org/url/... and go to the notebook rendering directly without the step of filling out the form.
If the page had been hosted at GitHub or another repository that MyBinder.org can use, you'd have in the upper right corner an additional icon looking like three rings on the nbviewer rendered page that could be clicked to open it as an active Jupyter notebook right in your browser without needing to login as it would be served vis MyBinder.org. The pages I link to for the Plotly plots and animation have this icon as an option.

Python: automatically handle download prompt when opening html file

Using Plotly I generate HTML files containing plots of data.
Plotly also provides an option that automatically asks the user to download the plot as .svg file when the HTML file is opened.
Is there a way to open these HTML files with python and then automatically save the generated .svg file to some location?

Accessing app-specific server-side generated files in django template

I have a django app (my_app) that based on the user query:
creates a file from the db
runs a program on the file from step-1 and gets an output file
generates a json file from the step-2 output file
renders a D3 visualization from a django template using the data from the json file from step-3
I need the program to run on the server side and the json file to be generated server-side as well.
Because the json files are query-specific, I thought it's not a good idea to keep these files in the /static/ folder and thought of keeping the files (even if temporarily) in e.g. /myapp/output_files/ folder.
The problem is that there is no url pattern corresponding to /myapp/output_files/my_file.json and I get a "Page not found (404)" error if I try to open the generated file and it obviously doesn't load in the javascript code in the template.
Is there a better way to design the system?
If the design is ok, how can I access a json file in the app's folder from the django template? Do I need something in the urls.py?
P.S. Everything works fine if I change the json location to /static/ or its subfolder.
Just add the location to your STATICFILES_DIRS setting as shown here
However, you probably need to build a view function that can somehow return the json based on some parameter in the url. Static files are meant to stay static...

How to export and preserve linked Jupyter notebooks?

I have multiple Jupyter notebooks that are linked to one another such that Notebook1.ipydb contains a link to Notebook2.ipydb with the markdown [Notebook2](Notebook2.ipynb) and vice versa.
When exporting all notebooks to HTML via nbconvert, the link to Notebook2.ipynb is preserved. I would like to change that link to the exported Notebook2.html so the linked HTML files function as a static website.
I tried to detect if I was running in iPython using get_ipython().__class__.__name__, but it executes this code before converting to HTML.
Is there a way to detect a static file to conditionally render the right markdown? Is there another way to preserve linked notebooks?
There's only really two options. One is to link to Notebook2.html in the first place and the other is to create a custom preprocessor for nbconvert.
from nbconvert.preprocessors import Preprocessor
import re
class CustomPreprocessor(Preprocessor):
def preprocess_cell(self, cell, resources, index):
if 'source' in cell and cell.cell_type == "markdown":
cell.source = re.sub(r"\[(.*)\]\(\1\.ipynb\)",r"[\1](\1.html)",cell.source)
return cell, resources
Save this to a file, then add to your nbconvert config file (located at ~/.jupyter/jupyter_nbconvert_config.py or can be generated using the command jupyter nbconvert --generate-config) the line:
c.HTMLExporter.preprocessors = ['CustomPreprocessor.CustomPreprocessor']
This assumes that the custom preprocessor file is named CustomPreprocessor and is located in the same directory as the files you're trying to convert. You could also properly install it as a module too.

Categories

Resources