Anaconda&jupyterntebook使用的一些问题

1. Anaconda环境的管理


基本命令

如何使用 conda 管理 Python 套件與環境

  • 列出所有环境
    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. 主题设置

  1. 直接在jupyternotebok里运行(加感叹号),若在Anaconda Prompt里运行 使用conda 命令。

    !pip install --upgrade jupyterthemes #更新 疑问已经安装了jupyterthemes模块,每次改主题都要安装一遍?调用的命令不知…`

    import jupyterthemes
    !jt -l
    
  2. 查看主题(GitHub内置的)
    !jt -l
    如图所示

  3. 当前我使用的设置为:
    !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】

CommandDescription 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 TCFONTtxtcell font 指定文本字体(见附录2)
-tfs TCFONTSIZE设置文本字体的大小
-dfs DFFONTSIZEpandas dataframe fontsize
-ofs输出区域的字号
-m MARGINS自动修复页边距
-cursw CURSORWIDTHset cursorwidth (px)
-cursc CURSORCOLORcursor color (r, b, g, p)
–vimtoggle styles for vim
-cellw CELLWIDTHset cell width (px or %) 占屏比。例:-cellw 80%:
-lineh LINEHEIGHTcode/text line-height (%)
-altpalt input prompt style
-altmdalt markdown cell style
-altoutset output bg color to notebook bg
-Phide cell input prompt
-Tmake toolbar visible 显示工具栏
-Nnb name/logo visible
-klkernel logo visible
-rreset to default theme
-dfontsforce fonts to browser default
-mathfs MATHFONTSIZEmathjax fontsize (in %)

附录1
附录1
附录2
附录3


配置后的效果是这样的,如下。大家觉得怎么样?
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())
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值