在浏览器中我们关闭正在训练的模型打印后,再次打开可以看到没有任何信息输出了。除了处理缓存方法,我们可以使用python的logging输出日志。
二、代码编写
import logging
import sys
import datetime
def init_logger(filename, logger_name):
@brief:
initialize logger that redirect info to a file just in case we lost connection to the notebook
@params:
filename: to which file should we log all the info
logger_name: an alias to the logger
# get current timestamp
timestamp = datetime.datetime.utcnow().strftime('%Y%m%d_%H-%M-%S')
logging.basicConfig(
level=logging.INFO,
format='[%(asctime)s] %(name)s {%(filename)s:%(lineno)d} %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(filename=filename),
logging.StreamHandler(sys.stdout)
# Test
logger = logging.getLogger(logger_name)
logger.info('### Init. Logger {} ###'.format(logger_name))
return logger
# Initialize
my_logger = init_logger("./ml_notebook.log", "ml_logger")
每次打印输出如:
my_logger.info("XXX")
再如记录上一个cell的时间和输出,打印到日志的文件里,比如在第一个cell里:
%%capture out
%%timeit
a = 1+1
然后在下一个cell里:
my_logger.info("capture & timeit: " + out.stdout)
就能知道上一个cell运行的时间和内容了。
jupyter notebook 如何在关闭浏览器后后台执行并保存结果? - 知乎 (zhihu.com)
logging — Logging facility for Python — Python 3.10.5 documentation
在浏览器中我们关闭正在训练的模型打印后,再次打开可以看到没有任何信息输出了。除了处理缓存方法,我们可以使用python的logging输出日志。每次打印输出如:再如记录上一个cell的时间和输出,打印到日志的文件里,比如在第一个cell里: 然后在下一个cell里:就能知道上一个cell运行的时间和内容了。参考:jupyter notebook 如何在关闭浏览器后后台执行并保存结果? - 知乎 (zhihu.com)logging — Logging facility for Pyt
在使用
jupyter
notebook
时,如果想改中间的内容,但有时你会发现后面的内容就没有了,不见了,
你需要重写这一行代码,这费时间。
这其实是设置的
问题
,在word中也会出现这种情况,是‘改写’
问题
。
在
jupyter
notebook
中,你只要按键盘上的 insert 键就
解决
这个
问题
了。
补充知识:
Jupyter
重新导入修改后的自定义包
Jupyter
经常遇到这样一个
问题
,就是在已有的
notebook
中导入了自定义的 itools.py 包文件,但是在编辑
notebook
中发现需要对 itools.py 文件某些函数进行修改,但是保存后再次运行 import itools
在用
python
编程的时候,你是否偶尔有个需求?需要把输出的
日志
or 信息清空?以下笔者将从3个角度讨论清空输出的简单方式
1.
Jupyter
notebook
下的输出清空
主要通过I
Python
.display.clear_output来清空
from I
Python
.display import clear_output as clear
print('before')
clear() # 清除输出
print('after')
2. Terminal/console下的输
场景是这样的,今天在使用jyputer
notebook
写代码时,因为代码比较长,刚好图书馆有外接的显示器可以用,所以在
浏览器
中打开了同一个
notebook
,手头的拿来写,另外一个只拿来看之前的代码。
but,刚才脑子抽了不小心把那个旧的
notebook
保存了,导致新写的代码都丢失了,心里一万只草泥马跑过啊,眼前发黑,两眼呆滞…
赶紧上网查找
解决
方案,找了一个可行的在这里贴出来。
因为我的
notebook
没有
关闭
,内核还在运行,所以可以这样
解决
:
在
notebook
中执行下面代码:
history
<h3>回答1:</h3><br/>anaconda调用
jupyter
notebook
无法
跳转
浏览器
,主要可能是系统环境配置出现
问题
所导致的。在此情况下,
解决
问题
的方法包括以下几个方面:
1.检查
浏览器
是否与anaconda配置的默认
浏览器
一致,如果不一致,则打开
jupyter
notebook
时可能
无法
正常跳转。可以在
浏览器
中进行相关设置,将其修改为默认
浏览器
。
2.检查anaconda的安装路径是否包含在系统的环境变量中,否则可能会导致
无法
正确调用相关模块。可以在系统环境变量中添加anaconda的安装路径,确保其被正确识别。
3.检查
jupyter
notebook
运行时是否存在权限
问题
,有时候可能需要以管理员身份运行
jupyter
notebook
才能正常跳转
浏览器
。
4.对于代码不运行的
问题
,可能是因为anaconda中需要的依赖项没有正确安装,可以使用conda命令
查看
各个包的状态并进行升级或重新安装。
综上所述,如果anaconda调用
jupyter
notebook
无法
跳转
浏览器
或者代码不运行,可以从
浏览器
设置、系统环境变量、权限以及包管理等多个方面进行排查和
解决
。