Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Section marks (#%%) not working as expected #11426

Closed
8 tasks done
abudden opened this issue Jan 29, 2020 · 4 comments · May be fixed by #14579 or #21706
Closed
8 tasks done

Section marks (#%%) not working as expected #11426

abudden opened this issue Jan 29, 2020 · 4 comments · May be fixed by #14579 or #21706

Comments

@abudden
Copy link

abudden commented Jan 29, 2020

Issue Report Checklist

  • Searched the issues page for similar reports
  • Read the relevant sections of the Spyder Troubleshooting Guide and followed its advice
  • Reproduced the issue after updating with conda update spyder (or pip, if not using Anaconda)
  • [N/A] Could not reproduce inside jupyter qtconsole (if console-related)
  • Tried basic troubleshooting (if a bug/error)
    • Restarted Spyder
    • Reset preferences with spyder --reset
    • [N/A] Reinstalled the latest version of Anaconda
    • Tried the other applicable steps from the Troubleshooting Guide
  • Completed the Problem Description, Steps to Reproduce and Version sections below

Problem Description

I've got a template that I've used for quite a long time for initial content. This is in %USERNAME%\.spyder-py3\template.py. In Spyder 4.0.1 I noticed that the section marks weren't working correctly. On investigation, it seems that an extra percent on the first section mark makes it work, which seems really weird to me.

Compare the two images below. In each image, the left-hand side shows the template.py in Vim; the right-hand side shows what happens when you then create a new file in Spyder 4.0.1. The only difference between the bare template files as shown in Vim in the two images is that there's an extra % on line 5: #%% at the start of the line on the first (top) image vs #%%% on the second (bottom) image. All the other section markers stay as #%%.

In the first image (with #%% on all sections), the sections aren't marked with a horizontal line in Spyder, in the second image (with #%%% on the first section but not the others), all the sections are marked.

noextrapercent

extrapercent

What steps reproduce the problem?

  1. Use the attached template as %USERNAME%\.spyder-py3\template.py (the attached file is the one without the extra % sign. I've uploaded it as template_py.txt as github won't let me upload it to an issue as a python file.
    template_py.txt
  2. Create a new file in Spyder.

What is the expected output? What do you see instead?

Should see section marks but don't.

Paste Traceback/Error Below (if applicable)

N/A

Versions

  • Spyder version: 4.0.1
  • Python version: 3.7.6 64-bit
  • Qt version: 5.9.6
  • PyQt version: 5.9.2
  • Operating System name/version: Windows 10

Dependencies

atomicwrites >=1.2.0         :  1.3.0 (OK)
chardet >=2.0.0              :  3.0.4 (OK)
cloudpickle >=0.5.0          :  1.2.2 (OK)
diff_match_patch >=20181111  :  20181111 (OK)
intervaltree                 :  None (OK)
IPython >=4.0                :  7.11.1 (OK)
jedi =0.14.1                 :  0.14.1 (OK)
nbconvert >=4.0              :  5.6.1 (OK)
numpydoc >=0.6.0             :  0.9.2 (OK)
pexpect >=4.4.0              :  4.8.0 (OK)
pickleshare >=0.4            :  0.7.5 (OK)
psutil >=0.3                 :  5.6.7 (OK)
pygments >=2.0               :  2.5.2 (OK)
pylint >=0.25                :  2.4.4 (OK)
pyls >=0.31.2;<0.32.0        :  0.31.2 (OK)
zmq >=17                     :  18.1.0 (OK)
qdarkstyle >=2.7             :  2.7 (OK)
qtawesome >=0.5.7            :  0.6.0 (OK)
qtconsole >=4.6.0            :  4.6.0 (OK)
qtpy >=1.5.0                 :  1.9.0 (OK)
rtree >=0.8.3                :  0.8.3 (OK)
sphinx >=0.6.6               :  2.3.1 (OK)
spyder_kernels >=1.8.1;<2.0.0:  1.8.1 (OK)
watchdog                     :  None (OK)
cython >=0.21                :  0.29.14 (OK)
matplotlib >=2.0.0           :  3.1.1 (OK)
numpy >=1.7                  :  1.18.1 (OK)
pandas >=0.13.1              :  0.25.3 (OK)
scipy >=0.17.0               :  1.3.2 (OK)
sympy >=0.7.3                :  1.5.1 (OK)

@ccordoba12
Copy link
Member

Thanks for the detailed report. Unfortunately this is very low priority to us, but you're welcome to send us a pull request to fix it.

@bcolsen
Copy link
Member

bcolsen commented Jan 29, 2020

This more or less a duplicate of this issue #6379

The template function uses %'s to identify replacement fields in the template to they get stripped from code cell section makers # %%. I think that {{username}} could work but would interfere with nested dictionaries. {%username%} might be the best but users might think that it would allow code execution in the replacement.

@ccordoba12
Copy link
Member

Ok, closing then.

@ccordoba12 ccordoba12 removed this from the future milestone Feb 1, 2020
@t-abraham
Copy link

How about having a section variable to make sections in the template which will be replaced to #%%

example template:
`# -- coding: utf-8 --
"""
Created on %(date)s

@author: %(username)s
"""

#%(section)s Initialization of Libraries

import sys, os, inspect

currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir)
parentdir_1up = os.path.dirname(parentdir)
sys.path.insert(0,parentdir_1up)

#%(section)s Standalone Run

if name == 'main':
pass`

Output new file:
`# -- coding: utf-8 --
"""
Created on Wed Jan 13 02:41:46 2021

@author: Tahasanul Abraham
"""

#%% Initialization of Libraries

import sys, os, inspect

currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
parentdir = os.path.dirname(currentdir)
sys.path.insert(0,parentdir)
parentdir_1up = os.path.dirname(parentdir)
sys.path.insert(0,parentdir_1up)

#%% Standalone Run

if name == 'main':
pass`

To that I have just edited the plugin.py file from Anaconda3\Lib\site-packages\spyder\plugins\editor\widgets at line 1825 where the Dictionary Variable VARS will hold 3 keys instead of two.

VARS = { 'date': time.ctime(), 'username': username, 'section': '%%', }

Adding a pull request if the developers like it they can take it in. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants