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

Predefined code cell in template is not shown up in new file #9919

Closed
9 of 10 tasks
phineas-pta opened this issue Jul 30, 2019 · 1 comment
Closed
9 of 10 tasks

Predefined code cell in template is not shown up in new file #9919

phineas-pta opened this issue Jul 30, 2019 · 1 comment

Comments

@phineas-pta
Copy link

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)
  • Could not reproduce inside jupyter qtconsole (if console-related)
  • Tried basic troubleshooting (if a bug/error)
    • Restarted Spyder
    • Reset preferences with spyder --reset
    • 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 use code cells a lot so I predefine code cells in template using #%% or # %%. But when I create new file, the code cells aren't here, there's only #% or # %

What steps reproduce the problem? What do you see instead?

  1. menu: Tools > Preferences > Editor > Advanced settings > Edit templates for new modules
  2. add a code cell:
    image
  3. create new file: the code cell definition disappears!
    image

workaround I found: add an additional % or use # <codecell>

Versions

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

Dependencies

IPython >=4.0     :  7.6.1 (OK)
cython >=0.21     :  None (NOK)
jedi >=0.9.0      :  0.13.3 (OK)
matplotlib >=2.0.0:  3.0.3 (OK)
nbconvert >=4.0   :  5.5.0 (OK)
numpy >=1.7       :  1.16.4 (OK)
pandas >=0.13.1   :  0.24.2 (OK)
pycodestyle >=2.3 :  2.5.0 (OK)
pyflakes >=0.6.0  :  2.1.1 (OK)
pygments >=2.0    :  2.4.2 (OK)
pylint >=0.25     :  2.3.1 (OK)
qtconsole >=4.2.0 :  4.5.2 (OK)
rope >=0.9.4      :  0.14.0 (OK)
sphinx >=0.6.6    :  2.1.2 (OK)
sympy >=0.7.3     :  1.4 (OK)
@bcolsen
Copy link
Member

bcolsen commented Jul 30, 2019

Thank you for the bug report!

This is an issue that I have had as well. It results from using % to template the strings in the template. When #2884 gets implemented with python string templating, this problem will be solved.

I'm closing this in favor of #2884, but feel fee to join in the conversation on that thread.

It looks fairly simple to implement if you want to give it a shot. The code that does the current templating is here:

try:
if text is None:
default_content = True
text, enc = encoding.read(self.TEMPLATE_PATH)
enc_match = re.search(r'-*- coding: ?([a-z0-9A-Z\-]*) -*-',
text)
if enc_match:
enc = enc_match.group(1)
# Initialize template variables
# Windows
username = encoding.to_unicode_from_fs(
os.environ.get('USERNAME', ''))
# Linux, Mac OS X
if not username:
username = encoding.to_unicode_from_fs(
os.environ.get('USER', '-'))
VARS = {
'date': time.ctime(),
'username': username,
}
try:
text = text % VARS
except Exception:
pass
else:
default_content = False
enc = encoding.read(self.TEMPLATE_PATH)[1]
except (IOError, OSError):
text = ''
enc = 'utf-8'
default_content = True
empty = True

@bcolsen bcolsen closed this as completed Jul 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants