文章目录
1. Anaconda环境的管理
基本命令
-
列出所有环境
conda env list
-
删除环境
conda env remove -n py3
上述表示删除环境名为py3的环境 -
切换环境
activate tensorflow
-
查看环境中已安装的包
conda list
-
批量安装库
2. jupyter扩展插件Nbextensions使用,主要解释jupyter中各种插件
jupyter扩展插件Nbextensions使用,主要解释jupyter中各种插件
-
jupyternotebook安装nb扩展插件
conda install -c conda-forge jupyter_nbextensions_configurator
(conda安装扩展,答主少写了一条命令
conda install -c conda-forge jupyter_contrib_nbextensions
,怪不得我装好后只有配置界面,没有任何扩展功能标签可以选择。另外主题好像只能修改配色没办法修改字体,用命令也改不了,最后是修改chrome高级设置里的等宽字体才实现的。)
3. Python·Jupyter Notebook各种使用方法记录·持续更新
Python·Jupyter Notebook各种使用方法记录·持续更新
4. 主题设置
-
直接在jupyternotebok里运行(加感叹号),若在Anaconda Prompt里运行 使用conda 命令。
!pip install --upgrade jupyterthemes #更新 疑问已经安装了jupyterthemes模块,每次改主题都要安装一遍?调用的命令不知…`
import jupyterthemes !jt -l
-
查看主题(GitHub内置的)
!jt -l
-
当前我使用的设置为:
!jt -t oceans16 -f fira -fs 12 -ofs 10 -tf robotosans -tfs 12 -nf robotosans -nfs 13 -cellw 80%
说明:
使用 oceans16主题 ;代码字体 fira 字号12;ofs输出字号 12?;文本字体 robotosans 字号12;笔记本字体robotosans字号13 , 代码块宽(占屏比或宽度) 80%
完整的命令格式为:jt [-h] [-l] [-t THEME] [-f MONOFONT] [-fs MONOSIZE] [-nf NBFONT] [-nfs NBFONTSIZE] [-tf TCFONT] [-tfs TCFONTSIZE] [-dfs DFFONTSIZE] [-m MARGINS] [-cursw CURSORWIDTH] [-cursc CURSORCOLOR] [-vim] [-cellw CELLWIDTH] [-lineh LINEHEIGHT] [-altp] [-altmd] [-altout] [-P] [-T] [-N] [-r] [-dfonts]
【关于字体库不知道支持中文吗 Noto Sans CJK Droid Sans Fallback】
Command | Description of Command Line options |
---|---|
jt | 安装主命令 |
-h | 直接调用 jt -h 显示所有命令解释 |
-l | 列出所有内置主题 |
-t | 安装主题 t后面跟需要的主题,例: -t oceans16 |
-f | 选取代码的字体(字体库叫做MONOFONT 见附录1), 例:-f fira |
-fs | 设置代码字体的大小,例 -fs 12 |
-nf NBFONT | 指定notebook的字体(见附录2),例: |
-nfs NBFONTSIZE | 设置notebook字体的大小 |
-tf TCFONT | txtcell font 指定文本字体(见附录2) |
-tfs TCFONTSIZE | 设置文本字体的大小 |
-dfs DFFONTSIZE | pandas dataframe fontsize |
-ofs | 输出区域的字号 |
-m MARGINS | 自动修复页边距 |
-cursw CURSORWIDTH | set cursorwidth (px) |
-cursc CURSORCOLOR | cursor color (r, b, g, p) |
–vim | toggle styles for vim |
-cellw CELLWIDTH | set cell width (px or %) 占屏比。例:-cellw 80%: |
-lineh LINEHEIGHT | code/text line-height (%) |
-altp | alt input prompt style |
-altmd | alt markdown cell style |
-altout | set output bg color to notebook bg |
-P | hide cell input prompt |
-T | make toolbar visible 显示工具栏 |
-N | nb name/logo visible |
-kl | kernel logo visible |
-r | reset to default theme |
-dfonts | force fonts to browser default |
-mathfs MATHFONTSIZE | mathjax fontsize (in %) |
配置后的效果是这样的,如下。大家觉得怎么样?
droidsans 字体显示不好
使用latosans
!jt -t onedork -f inconsolata -fs 12 -ofs 10 -tf latosans -tfs 13 -nf latosans -nfs 13 -cellw 80% -N -T -kl
5. 调用jupyter notebook文件内的函数一种简单方法
调用jupyter notebook文件内的函数一种简单方法
Jupyter Notebook 里自己编写的模块调取方法
在当前目录下保存以下代码(保存为Ipynb_importer.py文件),并且在其它代码中首先import这个模块(import Ipynb_importer
),之后再import其它模块。
import io, os,sys,types
from IPython import get_ipython
from nbformat import read
from IPython.core.interactiveshell import InteractiveShell
class NotebookFinder(object):
"""Module finder that locates Jupyter Notebooks"""
def __init__(self):
self.loaders = {}
def find_module(self, fullname, path=None):
nb_path = find_notebook(fullname, path)
if not nb_path:
return
key = path
if path:
# lists aren't hashable
key = os.path.sep.join(path)
if key not in self.loaders:
self.loaders[key] = NotebookLoader(path)
return self.loaders[key]
def find_notebook(fullname, path=None):
"""find a notebook, given its fully qualified name and an optional path
This turns "foo.bar" into "foo/bar.ipynb"
and tries turning "Foo_Bar" into "Foo Bar" if Foo_Bar
does not exist.
"""
name = fullname.rsplit('.', 1)[-1]
if not path:
path = ['']
for d in path:
nb_path = os.path.join(d, name + ".ipynb")
if os.path.isfile(nb_path):
return nb_path
# let import Notebook_Name find "Notebook Name.ipynb"
nb_path = nb_path.replace("_", " ")
if os.path.isfile(nb_path):
return nb_path
class NotebookLoader(object):
"""Module Loader for Jupyter Notebooks"""
def __init__(self, path=None):
self.shell = InteractiveShell.instance()
self.path = path
def load_module(self, fullname):
"""import a notebook as a module"""
path = find_notebook(fullname, self.path)
print ("importing Jupyter notebook from %s" % path)
# load the notebook object
with io.open(path, 'r', encoding='utf-8') as f:
nb = read(f, 4)
# create the module and add it to sys.modules
# if name in sys.modules:
# return sys.modules[name]
mod = types.ModuleType(fullname)
mod.__file__ = path
mod.__loader__ = self
mod.__dict__['get_ipython'] = get_ipython
sys.modules[fullname] = mod
# extra work to ensure that magics that would affect the user_ns
# actually affect the notebook module's ns
save_user_ns = self.shell.user_ns
self.shell.user_ns = mod.__dict__
try:
for cell in nb.cells:
if cell.cell_type == 'code':
# transform the input to executable Python
code = self.shell.input_transformer_manager.transform_cell(cell.source)
# run the code in themodule
exec(code, mod.__dict__)
finally:
self.shell.user_ns = save_user_ns
return mod
sys.meta_path.append(NotebookFinder())