简体   繁体   English

在将代码从sublime复制到jupyter笔记本时,识别会转换为奇怪的箭头

[英]Identation coverts to weird arrow while copying code from sublime to jupyter notebook

Copying this piece of code from sublime : 从sublime复制这段代码:

# Texts Texts Texts Texts Texts Texts 
for i in range(10):
    #Idented Texxt
    print i

results in below, with the line tab in jupyter-notebook. 结果如下,jupyter-notebook中的行选项卡。 Now if i want to add to the code and do further testing in jupyter, i need to copy this weird arrow instead of hitting tab (else, it shows indentation error). 现在,如果我想添加代码并在jupyter中进行进一步测试,我需要复制这个奇怪的箭头而不是点击标签(否则,它显示缩进错误)。 Is there some simple way to change? 有一些简单的改变方法吗? I can imagine I am missing something really simple and this might not be a problem at all. 我可以想象我错过了一些非常简单的东西,这根本不是问题。 I tried looking but found no mention of this. 我试过看,但没有提到这一点。 I can change all the indentation to space in sublime and this might work while copying but I rather work with tabs, to make codes readable. 我可以在sublime中将所有缩进更改为空格,这可能在复制时有效,但我更喜欢使用制表符,以使代码可读。

在此输入图像描述

Second image with double tabs, how it looks like in notebook. 带有双标签的第二张图片,它在笔记本中的样子。

在此输入图像描述

It looks like the reason that this is happening to you is due to a mismatch in the world view of tools (ie Sublime and Jupyter in this case) with regards to tabs versus spaces, a perennial dispute indeed. 看起来这种情况发生在你身上的原因是由于工具的世界观(即本案例中的Sublime和Jupyter)在制表符与空格方面的不匹配,确实存在长期争议。

In particular in Sublime you have translate_tabs_to_spaces turned off because you want to work with physical tab characters. 特别是在Sublime中,您关闭了translate_tabs_to_spaces ,因为您希望使用物理制表符。 So pressing Tab in Sublime will insert physical tab characters which are visualized via the tab_size setting. 因此,在Sublime中按Tab键将插入通过tab_size设置可视化的物理制表符。

On the other hand, according to this issue, Jupyter does not support indentation via tabs when you press the Tab key. 另一方面,根据此问题,当您按Tab键时, Jupyter不支持通过选项卡缩进 As such in Jupyter despite your best intentions, when you press Tab it's inserting spaces instead. 尽管你最好的意图是在Jupyter中,当你按Tab键时它会插入空格。 It does however support physical tabs if they're pasted in, which is why it appears the way it does above and why you have to manually paste physical tabs to get the input that you need. 但它确实支持物理标签,如果它们被粘贴,这就是为什么它出现在上面的方式以及为什么你必须手动粘贴物理标签以获得所需的输入。

Of these two tools, Sublime allows you to choose whether you use physical tabs or space characters, but Jupyter does not and always wants spaces. 在这两个工具中,Sublime允许您选择使用物理标签还是空格字符,但Jupyter不会并且总是想要空格。 The most expedient solution in that case would be to not use tabs in Sublime either and then everything would match up. 在这种情况下最方便的解决方案是不使用Sublime中的选项卡,然后一切都匹配。

Presumably that's not an option for you, so the next best solution would be to bridge the gap by having Sublime provide the data to Jupyter in a way that it expects, which is possible with a simple plugin. 据推测,这不是你的选择,所以下一个最好的解决方案是通过让Sublime以一种预期的方式向Jupyter提供数据来弥补差距,这可以通过一个简单的插件实现。

In Sublime, select Tools > Developer > New Plugin... and replace the stub with this code, then save in the default location that Sublime will prompt you with (your User package) with a recognizable name such as copy_with_spaces.py : 在Sublime中,选择Tools > Developer > New Plugin...并用此代码替换存根,然后保存在Sublime将使用可识别名称(例如copy_with_spaces.py )提示您(您的User包)的默认位置:

import sublime
import sublime_plugin


class CopyWithSpacesCommand(sublime_plugin.TextCommand):
    """
    Copy the selected text to the clipboard, replacing all tab characters with
    spaces based on the tab size in the current view.
    """
    def run(self, edit):
        self.view.run_command("copy")

        tab_size = self.view.settings().get("tab_size", 4)      
        text = sublime.get_clipboard().expandtabs(tab_size)
        sublime.set_clipboard(text)

This implements a new command copy_with_spaces which will perform a copy but modify the data on the way through so that any physical tab characters are replaced with the appropriate number of white space characters. 这实现了一个新的命令copy_with_spaces ,它将执行复制但在修改过程中的数据,以便用适当数量的空格字符替换任何物理制表符。

With that in place, you can add a custom key binding to use when you're copying code from Sublime to Jupyter to streamline things. 有了这些,您可以添加自定义键绑定,以便在将代码从Sublime复制到Jupyter时简化使用。 An example might be: 一个例子可能是:

{
    "keys": ["shift+ctrl+c"],
    "command": "copy_with_spaces",
}

After copying the scripts to Jupyter cells from sublime: 将脚本从sublime复制到Jupyter单元格后:

  1. Select all the lines with arrows 选择带箭头的所有线条
  2. Press Tab - This will remove the arrows and add a tab at the beginning of each line Tab键 - 这将删除箭头并在每行的开头添加一个选项卡
  3. Press Shift + Tab - This will delete all the added tabs in the step above Shift + Tab - 这将删除上述步骤中添加的所有选项卡

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM