-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Automatic import plugin #1099
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
Closed
Closed
Automatic import plugin #1099
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import type { AppConfig } from '../pyconfig'; | ||
import { robustFetch } from '../fetch'; | ||
import { Plugin } from '../plugin'; | ||
import { getLogger } from '../logger'; | ||
|
||
const logger = getLogger('automatic-import-plugin'); | ||
|
||
export class AutomaticImportPlugin extends Plugin { | ||
pyscriptTag = document.querySelector('py-script'); | ||
|
||
/** | ||
* Trim whitespace from the beginning and end of a string. | ||
* The text that we are getting has new lines and possibly | ||
* whitespace at the beginning and end of the line. Since | ||
* we want to match only import statements, we need to trim | ||
* the whitespace on each line, join them together and then | ||
* match each beginning of the line so the regex rule will | ||
* only match what we perceive as import statements. | ||
* | ||
* NOTE: This is not a perfect solution and I'm a bit worried | ||
* about the performance of this. I'm sure there is a better | ||
* way to do this. | ||
* | ||
* @param text - The text to trim | ||
*/ | ||
_trimWhiteSpace(text: string): string { | ||
const lines = text.split('\n'); | ||
return lines.map(line => line.trim()).join('\n'); | ||
} | ||
/** | ||
* | ||
* Use regex magic to capture import statements and add the | ||
* dependency(s) to the packages list in the config. | ||
* | ||
* @param text - The text to search for import statements | ||
* @param config - The config object to add the dependencies to | ||
* | ||
*/ | ||
_addImportToPackagesList(text: string, config: AppConfig) { | ||
// Regex encantation to capture the imported dependency into a | ||
// named group called "dependency". The rule will match any text | ||
// that contains 'import' or 'from' at the beginning of the line | ||
// or '\nimport', '\nfrom' which is the case when we invoke _trimWhiteSpace | ||
const importRegexRule = /^(?:\\n)?(?:import|from)\s+(?<dependency>[a-zA-Z0-9_]+)?/gm; | ||
|
||
text = this._trimWhiteSpace(text); | ||
|
||
const matches = text.matchAll(importRegexRule); | ||
// Regex matches full match and groups, let's just push the group. | ||
for (const match of matches) { | ||
const dependency = match.groups.dependency; | ||
if (dependency) { | ||
logger.info(`Found import statement for ${dependency}, adding to packages list.`); | ||
config.packages.push(dependency); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* In this initial lifecycle hook, we will look for any imports | ||
* in the <py-script> tag inner HTML and add them to the packages list. | ||
* | ||
* We are skipping looking into the src attribute to not delay the | ||
* preliminary initialization phase. | ||
* | ||
*/ | ||
configure(config: AppConfig) { | ||
if (config.autoImports ?? true) { | ||
// config.packages should already be a list, but | ||
// let's be defensive just in case. | ||
if (!config.packages) { | ||
config.packages = []; | ||
} | ||
|
||
this._addImportToPackagesList(this.pyscriptTag.innerHTML, config); | ||
} | ||
} | ||
|
||
/** | ||
* In this lifecycle hook, we will to see if the user has specified a | ||
* src attribute in the <py-script> tag and fetch the script if so. Then | ||
* we will look for any imports in the fetched script and add them to the | ||
* packages list. | ||
* | ||
* NOTE: If we are fetching the file from a URL and not from the local file | ||
* system, this will delay the download of the python interpreter. Perhaps | ||
* we should throw an error if the src attribute is a URL? | ||
* | ||
*/ | ||
beforeLaunch(config: AppConfig) { | ||
if (config.autoImports ?? true) { | ||
const srcAttribute = this.pyscriptTag.getAttribute('src'); | ||
if (srcAttribute) { | ||
logger.info(`Found src attribute in <py-script> tag, fetching ${srcAttribute}...`); | ||
robustFetch(srcAttribute) | ||
.then(response => response.text()) | ||
.then(text => { | ||
this._addImportToPackagesList(text, config); | ||
}) | ||
.catch(error => { | ||
logger.error(`Failed to fetch ${srcAttribute}: ${(error as Error).message}`); | ||
}); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import numpy as np | ||
import pandas as pd | ||
|
||
print(np.__version__) | ||
print(pd.__version__) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't you add
\s*
to the beginning of the regex and removetrimWhiteSpace
?