I'm currently working with Jupyter IPython Notebook.I would like to put my notebook under version control.
That's why, when I save and checkpoint a Notebook (.ipynb file), I would like the changes to also be saved and synchronized in the corresponding python script (.py file) in the same folder. (see picture below)
my_files
Does it have something to do with the version of Jupyter I am using? Or do I have to edit a config_file?
Thanks
You need to create jupyter_notebook_config.py in your config-dir.
if doesn't exists, execute below command from home directory :
./jupyter notebook --generate-config
Add paste following code in this file :
import os
from subprocess import check_call
c = get_config()
def post_save(model, os_path, contents_manager):
"""post-save hook for converting notebooks to .py and .html files."""
if model['type'] != 'notebook':
return # only do this for notebooks
d, fname = os.path.split(os_path)
check_call(['ipython', 'nbconvert', '--to', 'script', fname], cwd=d)
check_call(['ipython', 'nbconvert', '--to', 'html', fname], cwd=d)
c.FileContentsManager.post_save_hook = post_save
You will then need to restart your jupyter server and there you go!
Related
I try to run a code using Jupyter Notebook, and then use nbconvert to export the created notebooks to pdf, but it does not work.
For reference my code is like this (But I don't think it is related to my code directly?):
# Import the necessary libraries
import pandas as pd
import matplotlib.pyplot as plt
from nbconvert import PDFExporter
from nbformat import v4 as nbf
# Read the Excel file using Pandas
excel_file = pd.ExcelFile('survey_responses.xlsx')
# Use a loop to iterate over the sheets and generate pie charts and PDF files for each one
for sheet_name in excel_file.sheet_names:
# Read the sheet using Pandas
df = excel_file.parse(sheet_name)
# Create a new Jupyter notebook for the current sheet
notebook = nbf.new_notebook()
# Add a text cell to the Jupyter notebook
text = 'The following pie charts show the results of the survey for Class {}.'.format(sheet_name)
notebook['cells'].append(nbf.new_markdown_cell(text))
# Use the subplots function to create a figure with multiple subplots
fig, ax = plt.subplots(nrows=len(df.columns), ncols=1, figsize=(8, 6))
# Use a loop to iterate over the columns and generate a pie chart for each one
for i, question in enumerate(df.columns[1:], start=1):
responses = df[question].value_counts()
# Add a code cell to the Jupyter notebook
code = 'ax[{}].pie(responses, labels=responses.index)\nax[{}].set_title("{}")'.format(i, i, question)
notebook['cells'].append(nbf.new_code_cell(code))
# Use nbconvert to convert the Jupyter notebook to a PDF file
exporter = PDFExporter()
pdf, _ = exporter.from_notebook_node(notebook)
with open('{}.pdf'.format(sheet_name), 'wb') as f:
f.write(pdf)
Jupyter Note send me a pop-up window with title "Package Installation" and content enter image description here:
The required file tex\latex\pgf\basiclayer\pgf.sty is missing. It is a part of the following package: pgf. The package will be installed from.................
I click install, then it shows: enter image description here:
[I 14:00:51.966 NotebookApp] Kernel started: 4529aa41-ab04-45c9-ab04-a723aafffe41, name: python3
[IPKernelApp] CRITICAL | x failed: xelatex notebook.tex -quiet
Sorry, but C:\Users\EconUser\anaconda3\Library\miktex\texmfs\install\miktex\bin\xelatex.exe did not succeed.
The log file hopefully contains the information to get MiKTeX going again:
C:/Users/EconUser/AppData/Local/MiKTeX/2.9/miktex/log/xelatex.log
You may want to visit the MiKTeX project page, if you need help.
[I 14:02:52.109 NotebookApp] Saving file at /(Step By Step) Export PDF with texts, codes, and multiple plots.ipynb
I tried web browsing and followed the method in this guide: https://github.com/microsoft/vscode-jupyter/issues/10910, but it did not work either.
I also try to install pandoc and MikTex again in Jupyter Notebook at the beginning of the code
!pip install --upgrade pip
!pip install pandoc
!pip install MikTex
it shows:
Requirement already satisfied: pip in c:\users\econuser\anaconda3\lib\site-packages (22.3.1)
Requirement already satisfied: pandoc in c:\users\econuser\anaconda3\lib\site-packages (2.3)
Requirement already satisfied: ply in c:\users\econuser\anaconda3\lib\site-packages (from pandoc) (3.11)
Requirement already satisfied: plumbum in c:\users\econuser\anaconda3\lib\site-packages (from pandoc) (1.8.0)
Requirement already satisfied: pywin32 in c:\users\econuser\anaconda3\lib\site-packages (from plumbum->pandoc) (227)
ERROR: Could not find a version that satisfies the requirement MikTex (from versions: none)
ERROR: No matching distribution found for MikTex
I have no idea at all. Is Miketex outdated or why?
I am trying to use pyghsheets from within a Jupyter notebook and I do not get it to work, while the same piece of code works nicely from within ipython.
from pathlib import Path
import pygsheets
creds = Path(r"/path/to/client_secret.json")
gc = pygsheets.authorize(client_secret=creds)
book = gc.open_by_key("__key__of__sheet__")
wks = book.worksheet_by_title("Sheet1")
wks.clear(start="A2")
When called from within ipython everthing works fine, whereas from within a Jupyter notebook I get
RefreshError: ('invalid_grant: Token has been expired or revoked.', {'error': 'invalid_grant', 'error_description': 'Token has been expired or revoked.'})
I run both pieces from within the same conda environment. Any suggestion on how to narrow down the problem (and solutions) are very welcome!
It turns out that current working directory of my Jupyter notebook was not the same as the one of my plain ipython. Now pygsheets stores the token that is used for authentication with Google in the current working directory. If the .json file in that directory is invalid, authentication will fail.
You can add a parameter credentials_directory=... to specify a previously validated token file.
The solution I ended up with was
gc = pygsheets.authorize(client_secret=creds, credentials_directory=creds.parent)
That way token and credential files are in the same directory.
I am trying to import mpg dataset in Jupyter notebook. Here is my code:
import csv
%precision 2
with open('mpg.csv') as csvfile:
mpg = list(csv.DictReader(csvfile))
I am getting the error, saying: [Errno 2] No such file or directory: 'mpg.csv'
Can anyone point out what is wrong?
the problem is your jupyter notebook is not running in the same folder where your mpg.csv file is present. Technically, your present working directory is different.
To solve this try to add the full path of your csv file like this-
path = 'D:/xyx/xyz/.../mpg.csv'
and then pass this path in the open function. I hope this should work.
good luck ~~
jupyter online by default looking for your file in their own directory not in your local directories so first upload that file into jupyter
thank you
Try this, it should fix the problem:
with open('datasets/mpg.csv') as csvfile:
I am unable to setup & run a simple darkflow program. Infact can't even configure darkflow library:
from darkflow.net.build import TFNet
==> ModuleNotFoundError: No module named 'darkflow'
My Target is to run the following program:
from darkflow.net.build import TFNet
import cv2
options = {"model": "cfg/yolo.cfg", "load": "bin/yolo.weights", "threshold": 0.1}
tfnet = TFNet(options)
imgcv = cv2.imread("./test/dog.jpg")
result = tfnet.return_predict(imgcv)
print(result
Please suggest steps so that I could configure darkflow on Jupyter Notebook (with no GPU) and run the above code
Fixed by creating the file in ipynb file in darkflow directory (downloaded from github) and executing the following from the notebook:
!python3 setup.py build_ext --inplace
!pip install -e .
!pip install .
I have a python project which has to be exported in to an executable file so that it can be used in other systems as well. The project has its UI in a browser and runs on localhost.
So far I have tried PyInstaller and cx_Freeze but to no success. I encountered some errors with PyInstaller which I was unable to solve and I switched to cx_Freeze. I was able to freeze the scripts and create a .exe file. But when I open(double click) the .exe file, I get nothing. Not even an error message. I tried running it from command prompt as well, but there too I got no message or output.
Can anyone suggest how my objective can be achieved? Or something needs to be checked?
Here is my setup.py
import sys
import os
from cx_Freeze import setup, Executable
base = None
#if sys.platform == "win32":
# base = "Win32GUI"
os.environ['TCL_LIBRARY']="C:\\Users\\M******\\AppData\\Local\\Continuum\\Anaconda3\\tcl\\tcl8.6"
os.environ['TK_LIBRARY']="C:\\Users\\M******\\AppData\\Local\\Continuum\\Anaconda3\\tcl\\tk8.6"
setup ( name = "Network Analysis",
version = "0.1",
description = "Network Analysis Project",
options = { "build_exe": { "packages" : ['encodings','asyncio','pandas','numpy','geopy','networkx','configparser','json']}},
executables = [Executable("run.py",base=base)])