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

All modules in the same directory: #3

Open
JdeH opened this issue Jun 25, 2017 · 6 comments
Open

All modules in the same directory: #3

JdeH opened this issue Jun 25, 2017 · 6 comments

Comments

@JdeH
Copy link

JdeH commented Jun 25, 2017

Hi Michael,

I am curious about:

You'll want to arrange for the htmltree.py file to be in or symbolically linked in the same directory as any other python files to be transpiled as part of your project. That's a current limitation of Transcrypt. It's on the list of issues at the Transcrypt repo and the author, Jacques de Hooge, has it on his list of upcoming enhancements.

See my comment at:

TranscryptOrg/Transcrypt#337

In short, all of the following should work:

  1. Pip-install htmltree from PyPi and it will be found both by Transcrypt and CPython
  2. Put htmltree in Transcrypt's modules directory, it will be found only by Transcrypt
  3. Put it in the directory of your project (main file so to say), it will only be found by your project
  4. Add the htmltree directory (so the dir where htmltree.py resides) to your PYTHONPATH

To avoid the need for a symlink, 1 or 4 would be best. Since 1 isn't yet supported, 4 remains.

KR
Jacques de Hooge

@Michael-F-Ellis
Copy link
Owner

(the following is a duplicate of my reply to TranscryptOrg/Transcrypt#337)

@JdeH I'm sure the misunderstanding must be mine. After more than a decade of active Python development, I confess I still dont really understand the module/package/import system. I wrote the Transcrypt section of the README in more haste than I should have and with issue #321 in the back of my mind. BTW, thanks for the pointer in your earlier comment to Peter Downs's article on PyPi.

Re: your list of options

(pip) I like this for general users -- who may or may not be using Transcrypt. The only thing that's hanging me up is not knowing how to set things up so that users can do from htmtree import ... instead of from from htmltree.htmltree import ... after pip installing the package.

(Transcrypt modules) It's good to know about this one. I'll add it to the README.

(in the project) I've done this in a more elaborate way (with git submodule) in NearlyPurePythonWebAppDemo Using git submodule allows discretion to bring in new versions of the htmltree project. I still end up with having to import from htmltree.htmltree.

(PYTHONPATH) Have to say I've never liked environment variables like PYTHONPATH, but I'll certainly mention it. I find symlinks easy to create and delete at the command line without editing .bashrc or whatever is the WIndows equivalent.

My current thought is to update the README (once I've solved the htmltree.htmltree issue) with a recommendation to use option 1 and a link to a separate doc that enumerates and discusses the other options.

Cheers,
Mike

"There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch."

@Michael-F-Ellis
Copy link
Owner

Looks like https://stackoverflow.com/a/36515301/426853 has the answer to the htmltree.htmltree problem.

@JdeH
Copy link
Author

JdeH commented Jun 25, 2017

Indeed, I think using an __init__,py is what your looking for.

About obvious... It's very good that sometimes someone else points out to me that what seems to be obvious after having developed an arbitrary habit, isn't so obvious after all. I'm afraid in developing Transcrypt I've also developed some blind spots. Sometimes I forgot how to do something, want to look it up in the docs, but it isn't there, because at that time I thought it was 'obvious', or didn't think at all...

@JdeH
Copy link
Author

JdeH commented Jun 25, 2017

And about environment vars: I don't like the use of them as well. They get me entangled with something organically grown like Windows more than I feel comfortable with. Maybe Transcrypts module search path should be alterable in a different way.

@JdeH
Copy link
Author

JdeH commented Jun 25, 2017

I will try to come up with an alternative for 4 that doesn't use envir vars.
Since the module path has to be known compile time, something dynamic like sys.path might not be appropriate. Maybe a command line switch or pragma will work better.

@JdeH
Copy link
Author

JdeH commented Jun 25, 2017

In the newest version of TS, about to be committed, you'll be able to one of the following:

EITHER use the -xp / --xpath command line switch

transcrypt -b -xp .. client.py

OR use a pragma

# -*- coding: utf-8 -*-
"""
Description: Client side of sanity check
Uses JS functions insertAdjacentHTML, innerHTML and addEventListener.
See https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML
    https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML
    https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

Author: Mike Ellis
Copyright 2017 Owner
"""

# __pragma__ ('xpath', ['..'])

from htmltree import *
def start():
    console.log("Starting")
    ## insert a style element at the end of the <head?
    cssrules = {'.test':{'color':'green', 'text-align':'center'}}
    style = Style(**cssrules)
    document.head.insertAdjacentHTML('beforeend', style.render())

    ## Replace the <body> content
    newcontent = Div(H1("Sanity check PASS", _class='test'))
    document.body.innerHTML = newcontent.render()
    console.log("Finished")

## JS is event driven.
## Wait for DOM load to complete before firing
## our start() function.
document.addEventListener('DOMContentLoaded', start)

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

No branches or pull requests

2 participants